Reputation: 61
Im new to flutter and firestore. I´m building a social media app and need to get some userinformation from the current logged in user to the user profilepage. Surch as username, email etc. I have manage to get the userdata up to firestore, but Im struggling to get the current userinformation back to my project.
I've tryed this method, and it does get the userinformation back from firestore, but not the current userinformation unfortuinetly. I guess the plroblem is that Im stuck on the first document in my database on firestore?
StreamBuilder(
stream:
Firestore.instance.collection('userData').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return CircularProgressIndicator();
}
return Container(
child: Padding(
padding: const EdgeInsets.only(top: 75.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
snapshot.data.documents[0]['userName'],
style: TextStyle(fontSize: 25.0),
),
Padding(
padding: const EdgeInsets.only(top: 5.0),
child:
Text(snapshot.data.documents[0]['email']),
),
],
),
),
);
}),
I would really appreciate any potential solutions.
Thank you!
Upvotes: 5
Views: 14907
Reputation: 138
Future<void> _getUserName() async {
Firestore.instance
.collection('Users')
.document((await FirebaseAuth.instance.currentUser()).uid)
.get()
.then((value) {
setState(() {
_userName = value.data['UserName'].toString();
});
});
}
Add the _userName to the initstate like this
void initState() {
super.initState();
_getUserName();
}
and call it like this
child: Text('Hello $_userName,'),
I hope this will work
Upvotes: 6
Reputation: 408
If you are using Firebase authentication, then
import 'package:firebase_auth/firebase_auth.dart';
FirebaseUser user = await FirebaseAuth.instance.currentUser();
will get you the current logged in user. After that, you can do
var email = user.email;
var userName = user.displayName;
to get relevant user info.
Upvotes: 2