Home > database >  Flutter FutureBuilder does not stop showing CircularProgressIndicator
Flutter FutureBuilder does not stop showing CircularProgressIndicator

Time:08-17

I am trying to receive data using a FutureBuilder, but it hangs on the CircularProgressIndicator. I think it's remaining on ConnectionState.waiting but I'm not sure why.


  @override
  initState() {
    _widgetList = getWidgetList();
  }

  Stream<List<String>> getFriendUUIDS() => Firestore.friends
  .doc(gameManager.myself.uuid)
  .snapshots()
  .map((snapshot) => ((snapshot.data()?.keys)?.toList()) ?? []);

  Future<List<MomentWidget>> getWidgetList() async{
    List<MomentWidget> widgetList = [];
    Set<String> momentIds = Set();
    await for (final uuids in getFriendUUIDS()){
      for (String uuid in uuids){
        DocumentSnapshot<Map<String, dynamic>> values =  await Firestore.users
        .doc(uuid)
        .get();
        for (String momentId in values.data()?['moments'] ?? [] ){
          momentIds.add(momentId);
        }
      }
    }

    for (String momentId in momentIds){
      DocumentSnapshot<Map<String, dynamic>> values = 
      await Firestore.instance.collection('moments').doc(momentId).get();
      Map<String, dynamic>? data = values.data()!;
      String downloadURL = await storage.ref('moments/$momentId').getDownloadURL();
      MomentWidget widget = MomentWidget(numLikes: data['liked_by'].length ,
       location: data['location'],
        date: data['timestamp'],
        names: data['taggedFriends'].toString(),
        shotBy: data['taken_by'], image: NetworkImage(downloadURL));
      widgetList.add(widget);
    }
    return widgetList;
  }

  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return Container(
      height: size.height,
      width: size.width,
      child: FutureBuilder(
        future: _widgetList,
        builder: (context, AsyncSnapshot<List<MomentWidget>> snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.done:
              if (snapshot.hasError) {
                return Text(snapshot.error.toString());
              } else {
                return ListView.builder(
                    shrinkWrap: true,
                    scrollDirection: Axis.vertical,
                    itemBuilder: (context, pos) {
                      return snapshot.data![pos];
                    },
                  );
              }
            case ConnectionState.waiting:
              return Center(
                child: CircularProgressIndicator(),
              );
            default:
              return Text('Unhandled State');
          }
        }
      ),
      );
  }

I have tried to get the Future inside of initState(), and have tried to use snapshot.hasData instead, to no avail.

CodePudding user response:

I have encountered a similar problem. When building an object from json , if the types don't match , it can quietly fail. I do not think your widgetList is ever returned. In my case I had a variable "cost" that I thought would be of type int , however in the database it was of type String. It always quietly failed and never showed the markers on the map widget

So:

  1. Check how many times that loop of yours is executed. Probably only once and then it quietly fails

If the above happens:

  1. Makes sure the types of your variables match the ones from the database. Comment out every variable one by one to find where the problem is.

Let me know if it works

  • Related