Why can't i use the method data() in my streambuilder in flutter
116
Well to be clear if you don't specify the StreamBuilder
type it will be an <AsyncSnapshot>
by default
The solution was to put the StreamBuilder
as a <DocumentSnapshot>
StreamBuilder<DocumentSnapshot>(
stream: usersDb.doc(_auth.currentUser!.uid).snapshots(),
builder: (context, currentUserDocSnapshot) {
if (!currentUserDocSnapshot.hasData) {
return Center(
child: CircularProgressIndicator(
backgroundColor: Colors.deepPurple,
));
}
return Text(userSelectedSnapshot.data!["username"]);
},
);
You can then access the .data
method and access your document values
Author by
Arnaud NaNo
Updated on January 04, 2023Comments
-
Arnaud NaNo over 1 year
Why can't i use the method
.data()
?StreamBuilder( stream: usersDb.doc(widget.allUsersFromDb.docs[index]["uid"]).snapshots(), builder: (context, snapshot) { if (!snapshot.hasData) { return Center( child: CircularProgressIndicator( backgroundColor: Colors.deepPurple, )); } // List? invitedByArray = snapshot.data!.data() not working return Text("Invite"); }, ),
-
Wilson Toribio about 2 yearsWhat error is it showing when you use data()?
-
Arnaud NaNo about 2 yearsThe method data() isn't defined for the type Object
-
Arnaud NaNo about 2 yearsfound my issue the snapshot was an async snapshot by default i need to declare it as a documentSnapshot
-
-
Community about 2 yearsAs it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.