简体   繁体   English

如何在 flutter 的 stream 构建器中切换 Firestore 查询?

[英]How to switch firestore queries in stream builder in flutter?

I have 3 different queries to use on 3 different sets of conditions in StreamBuilder<QuerySnapshot> but not able to do so.我有 3 个不同的查询可用于StreamBuilder<QuerySnapshot>中的 3 组不同条件,但不能这样做。 Below is the code下面是代码

Query query;
if (a==b){
query = Firestore.instance.collection('collection1');
}

else if (c==d){
query = Firestore.instance.collection('collection2');
}
else if (e==f){
query = Firestore.instance.collection('collection3');
}

Inside Widget Build内部小部件构建

StreamBuilder<QuerySnapshot>(

    stream: query.snapshots(),
    builder: (context,snapshot){
      if (!snapshot.hasData){
        return Text("Loading");
      }

      return ListView.builder(
          itemCount: snapshot.data.documents.length,
          itemBuilder: (context, index){

            String names = snapshot.data.documents[index]['name'];
            List steps = List.castFrom(snapshot.data.documents[index]["steps"])




            return NameCard(names: names,steps: steps);

          });
    },

  )

I tried to implement the query with the stream builder, but not able to do so.我尝试使用 stream 构建器来实现查询,但无法这样做。 if there is only one query it works perfectly fine.如果只有一个查询,它工作得很好。 but when I tried to switch queries depending upon the conditions it does not work, it gives error of null snapshot但是当我尝试根据它不起作用的条件切换查询时,它会给出null snapshot错误

how should I achieve it??我应该如何实现它?

Instead of代替

stream: query.snapshots()

Try defining the stream separately and then assign it to StreamBuilder.尝试单独定义 stream 然后将其分配给 StreamBuilder。

Stream stream;

if (a==b){
    stream = Firestore.instance.collection('collection1').snapshots();
}
else if (c==d){
    stream = Firestore.instance.collection('collection2').snapshots();
}
else if (e==f){
    stream = Firestore.instance.collection('collection3').snapshots();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复 flutter firestore stream 构建器错误? - How to fix flutter firestore stream builder error? Stream builder 从 firestore 到 flutter - Stream builder from firestore to flutter 如何在 flutter stream 生成器中使用不同的 firestore 集合创建 where 条件? - How to make where condition with different firestore collection in flutter stream builder? 如何在 Firestore 中使用 Flutter(网络应用)流构建器 - How to use Flutter (web app) stream builder with Firestore Flutter Stream Builder with Firestore 返回空列表 - Flutter Stream Builder with Firestore return empty list 如何从 Firestore flutter 的参考字段中获取值并在 stream 构建器中显示? - How to get value from reference field in firestore flutter and display in stream builder? Stream 构建器在使用 Firestore 发布的 flutter 应用程序上返回 null - Stream builder returns null on released flutter app using firestore Flutter 表单生成器和firestore - Flutter Form Builder and firestore 如何从我的 firestore 文档中获取数据作为变量并使用它来指导我的流构建器在颤振中使用的路径 - How do I get data from my firestore document as a variable and use that to direct the path my stream builder uses in flutter 如何创建 Flutter stream 构建器? - How do I create Flutter stream builder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM