I make sign up system and managing user information.
When sign-up, users are required to write user name, and it's stored in firestore.
User data are stored like this;
And trying to get userName
with the code;
CollectionReference users = FirebaseFirestore.instance.collection('users');
return FutureBuilder<DocumentSnapshot>(
future: users.doc(FirebaseAuth.instance.currentUser!.uid).get(),
builder:
(BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.hasError) {
return Text("Something went wrong");
}
print("${!snapshot.hasData} ${!snapshot.data!.exists}"); // always "false true"
if (!snapshot.hasData && !snapshot.data!.exists) {
return Text("Document does not exist");
}
if (snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> data =
snapshot.data!.data() as Map<String, dynamic>;
return Text("User Name: ${data['userName']}");
}
return const CircularProgressIndicator();
},
);
However always return Text("Document does not exist");
is called, and userName
is not displayed.
Why doesn't userName
returned? I searched for a misspelling but couldn't find it.
This issue has taken a lot of time today. Thank you.