I'm new to flutter and I'm trying to get the specific document's fields. I've saved auth id in firestore and using that auth ID in where query I'm trying to access that document and accessing displayName and storing it in userName variable but when i user Database.userName where i want to show this field it displays null on screen.
class Database {
static String? userName;
static void showDisplayName() async {
var collection = FirebaseFirestore
.instance
.collection('Users');
try {
var docSnapshot = await collection
.where("email", isEqualTo:AuthService().currentUser!.uid)
.snapshots().listen((event) { userName = event
.docs[0]['displayName'];});
}on FirebaseException catch (e){
print(e);
} catch (error){
print(error);
}
Map<String, dynamic> data = docSnapshot as
Map<String, dynamic>;
userName = data['displayName'];
}
}
and displaying that username in drawer
accountName: Text("${Database.userName}")