简体   繁体   中英

get user data from list of uids - Flutter

I have a list of UIDs and I need to get the user name and PhotoURL for each user id that is inside that list. when I try to do it with a for loop I'm only getting the data for the first uid. how can I do this properly?

  Future<List<UserModel>> getUserDetailsById(List ids) async {
try {
  List<UserModel> users = [];
  for (var id in ids) {
    final DocumentSnapshot documentSnapshot =
        await _firestore.collection('users').doc(id).get();
    users.add(UserModel.fromMap(id, documentSnapshot.data()));
    return users;
  }
} catch (e) {
  print(e);
  return [];
}

}

return in a for loop will return you from the function

remove it from loop and add it after

Future<List<UserModel>> getUserDetailsById(List ids) async {
try {
  List<UserModel> users = [];
  for (var id in ids) {
    final DocumentSnapshot documentSnapshot =
        await _firestore.collection('users').doc(id).get();
    users.add(UserModel.fromMap(id, documentSnapshot.data()));
  }
 return users;
} catch (e) {
  print(e);
  return [];
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM