[英]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.