Reputation: 3
im trying to save data from cloud firestore into a variable. My data is basically user data ( username , email , phone number )
I'm able to call the snapshot and print it but i can't save the data from firebase into a variable. I assume that this is because at the time of trying to assign the value the variable the data is not returned from firebase as yet.
DocumentSnapshot userObject ;
db.collection('users').where("email", isEqualTo: "[email protected]").snapshots()
.listen((data) =>data.documents.forEach((doc) => userObject = doc));
print(userObject);
i expected the userobject to store the snapshot retrieved from the database but instead it is null.
Upvotes: 0
Views: 4491
Reputation: 103541
You have to use an async
method and await
keyword.
your_async_method () async {
final documents = await db.collection('users').where("email", isEqualTo: "[email protected]").getDocuments();
final userObject = documents.documents.first.data;
print(userObject);
}
Upvotes: 1