未处理的异常:断言失败:“平台接口不得使用`implements`实现”(Flutter、Dartisolate)

问题描述 投票:0回答:1

我尝试将 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 等以外的数据类型?感谢您的帮助!

flutter dart exception
1个回答
0
投票

为了解决这个问题,在你的顶级函数中,使用

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);}
© www.soinside.com 2019 - 2024. All rights reserved.