Home > Mobile >  Future is returning empty List
Future is returning empty List

Time:11-06

My below code is returning an empty list, even though it SHOULD return data corresponding to my data stored in my firestore database. Also to note: The otherUserId print statement is not getting printed. How can I fix this issue?

 Future<List<String>> getChattingWith(String uid) async {
  List<String> chattingWith = [];
  try {
    // create list of all users user is chatting with
    final rooms = await FirebaseFirestore.instance
        .collection('rooms')
        .where('users', arrayContains: uid)
        .get();
    for (final room in rooms.docs) {
      final users = room['users'] as Map<String, dynamic>;
      final otherUserId = users['uid1'] == uid ? users['uid2'] : users['uid1'];
      print("otherUserId999: $otherUserId");
      chattingWith.add(otherUserId);
    }
    print("chattingWith: $chattingWith");
    return chattingWith;
  } catch (error) {
    print("error: $error");
    return [];
  }
}

enter image description here

CodePudding user response:

This part of your code does not match your data structure:

final rooms = await FirebaseFirestore.instance
    .collection('rooms')
    .where('users', arrayContains: uid)
    .get();

Your users is a Map and not an array, so arrayContains won't work here. As said in your previous question, you have to use dot notation to test nested fields:

final rooms = await FirebaseFirestore.instance
    .collection('rooms')
    .where('users.uid1', isEqualTo: uid)
    .where('users.uid2', isEqualTo: otherValue)
    .get();

That

  • Related