【发布时间】:2021-10-08 07:22:24
【问题描述】:
我的 FutureBuilder 出现问题。它应该将自定义类“GeoPost”映射到来自 firebase 查询的“列表”,但在我的代码中的某处它没有将 Geopost 列表读取为非 Future
这是发生错误的代码:
FutureBuilder(
future: postsRef.get().then((doc) {
geoPosts = doc.docs.map((doc) async => await GeoPost.fromDoc(doc)).toList();
}),
builder: (context, snapshot) { }
自定义类和函数:
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: A value of type 'List<Future<GeoPost>>' can't be assigned to a variable of type 'List<GeoPost>?'.
【问题讨论】:
标签: firebase flutter flutter-futurebuilder