I wrote this piece of code and it is causing some problems:
static getFriendsRequests({required id}) async {
QuerySnapshot snapshot = await friendsRef
.doc(id)
.collection('received')
.get();
List<friendRequestItem> feedItems = [];
snapshot.docs.forEach((doc) async{
DocumentSnapshot extracted = await usersRef.doc(doc.reference.id).get();
MyUser tempUser = MyUser.fromJsonDocument(extracted);
String url = '';
tempUser.profilePictureURL=='' ? null : url=tempUser.profilePictureURL;
FriendRequest fr = FriendRequest(
userID: tempUser.userID, uniqueName: tempUser.uniqueName,
name: tempUser.name, mediaURL: url);
print(fr.uniqueName+fr.name+fr.userID+fr.mediaURL);
feedItems.add(friendRequestItem(friendReq: fr));
});
return feedItems;}
To help reading the code, the first query is to get a list of documents, each referring to a unique id. In the second, for each document I search again in the database for some data of the given id. Now I noticed that the problem should be related to the async methods because all the data I get are correct but probably the return happens before the end of the second method. How can I solve? Tell me if you need more infos about other parts of the code. Thank you