I have to query different firestore collections from a single flutter screen. A sample looks like
final membersRef = db.collection("members").doc(memberId);
final noOfEventsRef = db.collection('events').where('year', isEqualTo: thisYear).count();
final myAttendance = db.collection('events').where('year', isEqualTo: thisYear).where('attendees', arrayContains: widget.memberId).count();
final dailyQuoteRef = db.collection('daily-quote').doc('JVX9rjht1P6SK5rtg8');
So first query returns a single document from members collection, the second query returns the # of events of a particular year and so on.
As of now I use a big nested then() call to get the data and convert into a model before display.
membersRef.get().then((doc) {
final data = doc.data() as Map<String, dynamic>;
member = Member.fromJson(data);
noOfEventsRef.get().then((event) {
eventCount = event.count;
myAttendance.get().then((attendance) {
attendanceCount = attendance.count;
dailyQuoteRef.get().then((DocumentSnapshot doc) {
final finalQuote = doc.data() as Map<String, dynamic>;
quote = Quote.fromJson(finalQuote);
}).whenComplete(() {
setState(() {
loading = false;
});
});
});
});
},
onError: (e) => print("Error getting document: $e"),
);
This feels like callback hell. How to properly code this!? Help much appreciated. Thanks
2
Answers
Try using await that will make code more readable and easy to understand
call getdata() from initstate.
An alternative solution is to use async/await with Future.wait()