[英]Flutter give Streambuilder initalData
I have a Streambuilder
that takes Firebase Firestore snapshot as a Stream and I would like to add initalData
to the Streambuilder
.我有一个Streambuilder
将 Firebase Firestore 快照作为 Stream ,我想将initalData
添加到Streambuilder
。
How can I pass initalData
to the Streambuilder
?如何将initalData
传递给Streambuilder
?
The Code:编码:
StreamBuilder(
stream: FirebaseFirestore.instance
.collection("events")
.snapshots(),
builder: (BuildContext ctx, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData || snapshot.data.docs.isEmpty) {
return NoDataRelatedLocation();
}
if (snapshot.hasError) {
return Text(snapshot.error.toString());
} else {
return new RelatedLocationListing(
relatedLocationList: snapshot.data.docs,
);
}
},
),
You can add initialData
to StreamBuilder
:您可以将initialData
添加到StreamBuilder
:
StreamBuilder(
initialData: ..... // <~~~ add it here.
stream: ...
builder: ...
You just need to make sure that your initialData
matches that type of data coming from the stream.您只需要确保您的initialData
与来自 stream 的数据类型相匹配。 Since QuerySnapshot
is a Firebase specific type, you should map your stream to a data type that you can create and that's known to you.由于QuerySnapshot
是 Firebase 特定类型,因此您应该将 map 您的 stream 转换为您可以创建并且您知道的数据类型。
Here's a pseudo code:这是一个伪代码:
initialData: [MyDataType()],
stream: FirebaseFirestore.instance
.collection("events")
.snapshots().map((snapshot) => MyDataType.fromMap(snapshot.doc));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.