Flutter: Firebase - How to get a DocumentSnapshot's Stream with StreamBuilder?
There is a particular document in my database I want to listen to with a StreamBuilder. I red, FuturBuilder is for tasks that shall only be executed once (for those people like me which did not know before). That's why I switched to StreamBuilder.
My setup is as follows:
StreamBuilder(
stream: FirebaseFirestore.instance
.collection('einstellungen')
.doc('lvgPLifWpRNZrEq3n1Je')
.snapshots(),
builder: (
context,
snapshot
) {
if (snapshot.hasData) {
if (snapshot.connectionState ==
ConnectionState.active) {
return Padding(
padding: const EdgeInsets.fromLTRB(
15.0, 5.0, 15.0, 0),
child: Test("Test")
);
}
}
return Container();
})
My issues:
-
If I print myself out the current
ConnectionStateof theStreamBuilder, I usually only getwaitingwhich I do not understand about why. -
In case I manage to make it into my if statements (is .active even correct? Shouldnt it be .done?), I am not able to get data from my map called "haushalt". I tried it in kinda endless combinations but everytime I get the standard "nullcheck" message. And even if I try to get along with it using either
!or??(last one to set a default value), the message still does not go away.
Some example combinations I used/tried out:
map = snapshot.data!.data()["household"]
map = snapshot.data!["household"]
map = snapshot.data!.data()[0]["household"]
map = snapshot.data!["household"] ?? "Test"
- Another message I keep getting is about
Streamnot being aFuture. Yeah, right, butStreamBuilderexpects aStream. If I switch back toFutureBuilderI come back to my initial issue where I was only able to paint my widget once.
Looking forward to your replys!
Update
I tried another approach by using where() and get() even though I refuse to use where() just because I was not able to query a single document by its auto generated id...
But even with this approach, I am stuck in the for loop now, because it blames be I am not returning a MapEntry bla bla bla
I am really starting to get frustrated yet again.
I do not GET the difference between when I can/have to use get(), get("field") and a snapshot(). I mean, in this example, I am using get() and the guy I have to code from still wrote snapshot....
This is just so fu...confusing^^
void getEinstellungen() async {
var collection = FirebaseFirestore.instance.collection("einstellungen");
var querySnapshot = await collection
.where("auto_id", isEqualTo: "lvgPLifWpRNZrEq3n1Je")
.get();
for (var snapshot in querySnapshot.docs) {
snapshot.data().map((key, value) {
print(key);
print(value);
});
}
}
Comments
Post a Comment