Issue
I am having an issue with my FutureBuilder in flutter. It’s supposed to map the custom class ‘GeoPost’ to a ‘List’ from a firebase query but somewhere in my code it is not reading the Geopost list as a non Future
here is the code where the error occurs:
FutureBuilder(
future: postsRef.get().then((doc) {
geoPosts = doc.docs.map((doc) async => await GeoPost.fromDoc(doc)).toList();
}),
builder: (context, snapshot) { }
custom class and functions:
class GeoPost {
final String caption;
final String postId;
final String authorId;
final int likesCount;
final int commentCount;
final Position position;
final Timestamp timeStamp;
final String? imageUrl;
final Userdata authordata;
GeoPost(this.caption, this.postId, this.authorId, this.likesCount, this.commentCount, this.position, this.timeStamp, this.imageUrl, this.authordata);
static Future<GeoPost> fromDoc(DocumentSnapshot doc) async {
return GeoPost.fromDocument(doc, await getPostUserDataFromFirestore(doc['userId']));
}
factory GeoPost.fromDocument(DocumentSnapshot doc, Userdata author) {
return GeoPost(
doc['caption'],
doc['postId'],
doc['userId'],
doc['likeCount'] ?? 0,
doc['commentCount'] ?? 0,
doc['position'],
doc['timeStamp'],
doc['imageUrl'],
author
);
}
Future<void> getPostUserDataFromFirestore (String did) async {
return Userdata.fromDoc(await usersRef.doc(uid).get());
}
}
error:
Error: A value of type 'List<Future<GeoPost>>' can't be assigned to a variable of type 'List<GeoPost>?'.
Solution
-
You need to wait for all the Futures in the
List<Future<GeoPost>
(which converts them into aList<GeoPost>
) before assiging them to a variable of typeList<GeoPost>
.Update your
FutureBuilder
code to this:FutureBuilder<List<GeoPost>>( future: () async { var doc = await FirebaseFirestore.instance.collection('collectionPath').get(); var data = doc.docs.map((doc) async => await GeoPost.fromDoc(doc)).toList(); geoPosts = await Future.wait(data); return geoPosts; }(), builder: (context, snapshot) { }
-
Also, you need to change the return type of the
getPostUserDataFromFirestore
method fromFuture<void>
toFuture<Userdata>
.void
is used when there is no value returned.Update the
getPostUserDataFromFirestore
method to this:Future<Userdata> getPostUserDataFromFirestore (String did) async { return Userdata.fromDoc(await usersRef.doc(uid).get()); }
Answered By – Victor Eronmosele
Answer Checked By – Gilberto Lyons (FlutterFixes Admin)