首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在dart中重写此代码以避免强制转换错误

如何在dart中重写此代码以避免强制转换错误
EN

Stack Overflow用户
提问于 2021-04-17 16:05:39
回答 1查看 46关注 0票数 0

我有这段代码,我想返回一个documentshapshot流。

代码语言:javascript
复制
/* Get Firestore products docs
 */
  Stream<List<DocumentSnapshot>> fetchFirstProductListStream() {
    
    ....
   return  getFirestoreUserStream(loggedInUser.uid).map((UserModel rad) => 
    (geo
          .collection(collectionRef: collectionReference)
          .within(center: center, radius: rad.radius, field: field)) as List<DocumentSnapshot>
    );
}

当我尝试将返回值转换为List时,我确实得到了以下错误。

代码语言:javascript
复制
> [VERBOSE-2:ui_dart_state.cc(186)] Unhandled Exception: type
> '_AsBroadcastStream<List<DocumentSnapshot>>' is not a subtype of type > > 'List<DocumentSnapshot>'
EN

回答 1

Stack Overflow用户

发布于 2021-04-17 20:24:07

您有两个选项:

Option1:,因为您最终已经将数据转换为普通列表。直接返回。您现在不需要StreamStreamBuilder来解析它。您可以直接使用ListView.builder()来呈现数据:

代码语言:javascript
复制
List<DocumentSnapshot> fetchFirstProductListStream() {
    
....
   return  getFirestoreUserStream(loggedInUser.uid).map((UserModel rad) => 
    (geo
          .collection(collectionRef: collectionReference)
          .within(center: center, radius: rad.radius, field: field)) as List<DocumentSnapshot>
    );
}

选项2:从接口返回一个真正的Stream,并使用StreamBuilder异步读取它:

签出此SO Post

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67136023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档