我尝试将 Firestore QuerySnapshot 中的数据解析为名为“Post”的数据模型。为了提高性能,我尝试使用compute()来使用dart隔离
我这里有这个代码:
static Future<List<Post>> getPosts(QuerySnapshot snapshot) async {
return await compute<QuerySnapshot, List<Post>>(_calculate, snapshot);
}
顶级函数:
List<Post> _calculate(QuerySnapshot snapshot) {
for (QueryDocumentSnapshot snap in snapshot.docs) {
//...Can't continue because this gives the error
}
return [];
}
错误图片:
是否可以处理除 int、String 等以外的数据类型?感谢您的帮助!
为了解决这个问题,在你的顶级函数中,使用
List<Post> _calculate(List<QueryDocumentSnapshot<Object?>> snapshots) {
...}
和你的函数调用:
static Future<List<Post>> getPosts(QuerySnapshot snapshot) async {
return await compute<List<QueryDocumentSnapshot<Object?>>, List<Post>>(_calculate, snapshot.docs);}