I am trying to display the name to the appbar but userid part seems like giving an error. iam a beginner to flutter and firestore can someone help me
class Profile extends StatefulWidget {
@override
_ProfileState createState() => _ProfileState();
}
class _ProfileState extends State<Profile> {
//To retrieve the Userid
User? user;
Future<void> getUserData() async {
User userData = await FirebaseAuth.instance.currentUser!;
setState(() {
user = userData;
print(userData.uid);
});
}
///////////////////////////////////////
Future<String>? _title;
@override
void initState() {
getUserData().then((value) => _title = _getAppBarNameWidget());
// _title = _getAppBarNameWidget();
super.initState();
}
//To retrieve the name from firestore
Future<String> _getAppBarNameWidget() async =>
await FirebaseFirestore.instance
.collection('customer')
.doc(user!.uid)
.get()
.then((DocumentSnapshot ds) async {
var name = ds['name'];
return name;
});
CodePudding user response:
DocumentSnapshot ds
does not directly contain document data, only document id, you have to use data()
function to get data. It is also a good idea to check whether the document really exists, because you will get a snapshot event if the document is not found.
Examples:
ds.exists // will return true if document is found
ds.id // will return document reference
ds.data()!['name'] // will return 'name' field of document