Reputation: 49
I have a variable called _memories and I want to update it to the content that is returned from the Firestore database in the .then() and then return it. I know the content is correct in from using the print statement, but neither return statement is returning the updated _memories. It's returning the {'title': 'myTitle'}. Anyone know how to fix this?
List getUserMemories() {
User? currentUser = AuthenticationService().getUser();
if (currentUser == null) {
throw Exception('currentUser is null');
}
CollectionReference memories = _firestore.collection('memories');
List _memories = [
{'title': 'myTitle'}
];
memories
.where('user_email', isEqualTo: currentUser.email)
.get()
.then((QuerySnapshot querySnapshot) async {
_memories = await querySnapshot.docs.map((e) => e.data()).toList();
print("In FirestoreService: $_memories");
return _memories;
})
.catchError((error) => print("Failed to obtain user's memories: $error"));
return _memories;
}
Upvotes: 1
Views: 159
Reputation: 9744
Try to convert getUserMemories
to an async
function and use await
where you call it:
List getUserMemories() async {
User? currentUser = AuthenticationService().getUser();
if (currentUser == null) {
throw Exception('currentUser is null');
}
CollectionReference memories = _firestore.collection('memories');
List _memories = [
{'title': 'myTitle'}
];
try {
final result = await memories
.where('user_email', isEqualTo: currentUser.email)
.get();
_memories = result.docs.map((e) => e.data()).toList();
print("In FirestoreService: $_memories");
} catch (e) {
print("Failed to obtain user's memories: $e"));
}
return _memories;
}
Upvotes: 1