0

I am trying to display club data for an app I'm working on in flutter using firebase. I'm trying to iterate over each club the user is in, and then get the data for each of those clubs and then output them in a ListView. Inside the foreach loop that I have to iterate over the clubs, I get the club data and add it to a list of maps (for key/value pairs), but then outside of the loop the list isn't updated. I initialized the list variable outside of the function so I'm stumped as to why it's not updating the list. Nothing seems to be asynchronous either so I don't believe that would be an issue either, unless I'm mistaken about that. Any help is appreciated, and here's the relevant code:

body: FutureBuilder(

      future: FirebaseDatabase.instance.reference().child('Users/${user.uid}/Clubs').once(), //clubRef.once(),
      builder: (context, AsyncSnapshot<DataSnapshot> snapshot) {
        if (snapshot.hasData) {
          _lists.clear();

          Map<dynamic, dynamic> values = snapshot.data.value;
          values.forEach((key, values) {
            FirebaseDatabase.instance.reference().child("Clubs/$values").once().then((DataSnapshot snapshot2) {
              if (snapshot.hasData) {
                Map<dynamic, dynamic> valuesClub = snapshot2.value;
                _lists.add(valuesClub);
                print(_lists);
              }
            });
          });

          print(_lists.length);
          return new ListView.builder(
              shrinkWrap: true,
              itemCount: _lists.length,
              itemBuilder: (BuildContext context, int index) {
                return Card(
                  child: InkWell(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text("Name: " + _lists[index]["Name"]),
                        Text("Description: " + _lists[index]["Description"]),
                      ],
                    ),
                    onTap: () {
                      print(index);
                      String clubId = values.keys.toList()[index];
                      Navigator.of(context).push(MaterialPageRoute(builder: (context) => ClubPage(clubId)));
                    },
                  )
                );
              });
        }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • You are calling `Future.then`. Anything involving a `Future` is inherently asynchronous. [Don't use `Map.forEach` with asynchronous callbacks.](https://stackoverflow.com/q/63719374/) – jamesdlin Feb 25 '21 at 19:30
  • @jamesdlin so I refactored with just a for instead of a foreach, and also put the for loop with the Future.then in a separate async function that's called in the future: xxxx part of the FutureBuilder. But it still isn't waiting for that async function to complete before building the widgets. – Zachary McDaniel Feb 25 '21 at 21:20
  • Post your new code. Are you `await`ing all `Future`s? Make sure you analyze your code with the `unawaited_future`s lint enabled. – jamesdlin Feb 25 '21 at 21:40

0 Answers0