Am trying to get data for a specific user only using flutter and firestore latest version.
Since firebase has been updated the latest version does’t work with the methods I’ve seen online.
Here’s my stream builder
final currentUser = FirebaseAuth.instance.currentUser;
//Streams are used to listen to realtime streams of data
@override
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: _usersStream,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return ListView(
children: snapshot.data!.docs.map((DocumentSnapshot document) {
Map<String, dynamic> data =
document.data()! as Map<String, dynamic>;
return Row(
children: [
Text(data['first name']),
Text(data['last name']),
],
);
}).toList(),
);
},
);
}
I want to do queries with the ‘uid’, This gets all the current users data.
I tried this as the stream query but doesn’t work
final Stream<QuerySnapshot> _myUserStream = FirebaseFirestore.instance.collection('users').doc(currentUser!.uid).snapshots(); your text
I also tried this
final Stream<QuerySnapshot> _usersStream = FirebaseFirestore.instance.collection('users').where('email', currentUser.email).snapshots();
2
Answers
For those wonder what I did.
First I get the user id of the current user.
The when am creating a profile I pass that id to the user data.
The
databaseService
is a custom class with the database functions. Here's thedatabaseService
class addUser(userProfile)
function.UserProfile
is the model that takes the variables.Here's the user profile class also.
So when am query the data base for a specific user.
The data I get from the stream has to be used in a streambuilder to access that data. so
_usersStream
is like the query and the streambuilder displays data from that query.Now to extract that data, check if the data is received successfully. The data received is passed to a
Map
. I can then convert that Map to anObject
.Object
would be like a class which in my case isUserProfile
.I can then use the
userData
to do what I want, display information etc. Pass to other screens all of that.Lets say Users is a model you made.
in my case its this.
}
You can get the User data using Future this way
enter image description here
For Streams this way