Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – Future is returning empty List

Posted on November 11, 2022 By

Solution 1 :

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 my answer to 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 is closest to what you tried in your previous question: Firestore conditional array query. It performs an AND condition on the uid1 and uid2 subfields of users.

If instead you want to get all rooms that the user is a participant in, you need an (additional) field that is an array with the UIDs of all participants.

participantUIDs: ["uid1", "uid2"]

Then you can do:

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

Problem :

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

Comments

Comment posted by Ozan Taskiran

So it jumps in the catch block? What is the error?

READ  [FIXED] android - How can I create my own custom marker on google map like google map's default marker
Powered by Inline Related Posts

Comment posted by cipano

No, it just prints “chattingWithList: []”

Comment posted by Ozan Taskiran

So your room.docs is empty, can you check it?

Comment posted by cipano

No, it is not empty. I am trying to fetch all rooms where the users array contains the uid. After that I create a list of the user ids which are not equal to the uid. So basically I want to create a list of all uids which a user is chatting with. But the data is fine, just the query is not working.

Comment posted by cipano

I have added an image of the users array. Maybe my query is wrong?

Comment posted by cipano

The GOAT himself. Thank you a lot for your time and help! Learned a lot today thanks to you!

Android Tags:android, dart, flutter, google-cloud-firestore

Post navigation

Previous Post: [FIXED] android – Overlapping items in RelativeLayout with ImageView and TextView
Next Post: [FIXED] android – Unable to instantiate application com.company.app.MainApplication: java.lang.ClassNotFoundException React Native Expo

Related Posts

[FIXED] android – React native 0.61.5 Crashlytics missing libhermes.so: SoLoader.java com.facebook.soloader.SoLoader.assertInitialized Android
[FIXED] android – Jetpack Compose detectDragGestures seems to be working only when Canvas modifier is set to fillMaxSize or matchParentSize inside a Box Android
[FIXED] android fragments – ListView ID called on null? (Kotlin) Android
[FIXED] java – how to pass a request parameter in retrofit post request Android
[FIXED] xamarin.forms – Xamarin Forms Using page transitions Android
[FIXED] How can I print out the results of this json file? (Android Retrofit2) Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can VPN be traced by police?
  • Where were Kaiser-Frazer cars built?
  • How do you make gold rose gold paint?
  • What are the newest type of dentures?
  • Can you wear joggers as dress pants?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme