Reputation: 17
I'm new to Firebase and I managed to finish authentication in my flutter app using Firebase as cloud database and it works perfectly fine. But how do I get the current user data? My situation/problem goes something like this.
the situation is like I logged in Facebook but I entered someone else's profile because I read the same collection and document. This is my code of how it goes
CollectionReference users = FirebaseFirestore.instance.collection('Harris');
return FutureBuilder<DocumentSnapshot>(
future: users.doc('username').get(),
builder: (BuildContext context,
AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.hasError) {
return Center(child: Text("Something went wrong"));
}
if (snapshot.hasData && !snapshot.data!.exists) {
return Center(child: Text("Document does not exist"));
}
if (snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> data =
snapshot.data!.data() as Map<String, dynamic>;
return Container(
height: double.infinity,
width: double.infinity,
decoration: BoxDecoration(
color: brightGray,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Welcome,',
style: ktextFont3,
),
Text(
'${data['username']}',
style: ktextFont4,
),]));}
return Center(
child: CircularProgressIndicator(
color: mintGreen,
));
},
);
this is my Firebase Console
I have tried to replace the collection and document with username variable but it shows null when I hot restart the app. Any advice/assist is appreciated. Thank you
Upvotes: 1
Views: 178
Reputation: 1357
If userA signed in you can get name,email,firebase uid and image from this line of code
Firebase id : FirebaseAuth.instance.currentUser!.uid
Firebase name : FirebaseAuth.instance.currentUser!.displayName
Firebase email : FirebaseAuth.instance.currentUser!.email
Firebase photo_url : FirebaseAuth.instance.currentUser!.photoURL
Upvotes: 1