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)));
},
)
);
});
}