Reputation: 1052
I have a Streambuilder
that takes Firebase Firestore snapshot as a Stream and I would like to add initalData
to the Streambuilder
.
How can I pass initalData
to the 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,
);
}
},
),
Upvotes: 1
Views: 666
Reputation: 2974
You can add initialData
to 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. 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.
Here's a pseudo code:
initialData: [MyDataType()],
stream: FirebaseFirestore.instance
.collection("events")
.snapshots().map((snapshot) => MyDataType.fromMap(snapshot.doc));
Upvotes: 1