Reputation: 1150
I have a FutureBuilder
widget that should wait for data from a firestore collection.
class MyScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: calendarQuery.getCalendarEntry(dateString),
builder: (BuildContext context, AsyncSnapshot snap) {
if (snap.hasData) {
List<Events> recipe = snap.data;
return Scaffold(
appBar: AppBar(
title: Text("Events"),
),
body: Column(
children: <Widget>[
...,
],
),
);
} else {
return LoadingScreen();
}
},
);
}
}
I retrieve a list of events and then for each event I need to fetch some additional details. I tried to do nested Futures and came up with the code below. It generates a Future<Iterable<Future<Detail>>>
which ends up as MappedListIterable<DocumentSnapshot,Future<Recipe>>
in snap.data
and i cannot handle it.
class CalendarQuery<T> {
...
Future<Iterable<Future<Detail>>> getCalendarEntry(String date, String type) async {
return await ref
.where("date", isEqualTo: date)
.getDocuments()
.then((data) {
return data.documents.map((doc) => Document<Details>(path: 'detailCollection/${doc.data["Event"]["SomeId"]}')
.getData());
});
}
}
I think I went wrong here at some points with handling the futures and there is probably a proper way to do this.
Does someone know a way to refactor getCalendarEntry
so that it returns a Future<List<T>>
? Or maybe there is a better approach to solve this?
Upvotes: 0
Views: 742
Reputation: 8714
You can use Future.wait
to create a Future that completes once all Futures from an Iterable
have completed.
In your scenario, you would replace return await ref
with return Future.wait(ref
and a closing bracket where needed, to create a Future that waits for all Details
to be retrieved.
Upvotes: 1