0
votes

I'm new in Flutter

I`m get the data from Firestore DB and show it as a ListTile.

Code on the pic

enter image description here

and here

enter image description here

Screenshot from app here

enter image description here

I save userID for each document in DB.

How can I filter and show only the active user's data?

I need the simplest and freshest solution.

userID will be hidden later

files with code here

1
Please don't post screenshots of your code, or other textual content. Instead post the actual text, and use the formatting tools of Stack Overflow to mark it up. Aside from that, have a look at: firebase.google.com/docs/firestore/query-data/queries - Frank van Puffelen

1 Answers

0
votes

Hi There I would filter based on userId. Lets assume you want to get favourite user places, this user places will be a sub collection of the users one. Therefore I will do my filter as follow:

'''

    // Get User Favourite Places.
    Future<List<UserFavPlaces>?>? getUserFavouritePlaces(
          {required String userId}) async {
        final userFavouritePlaces = await FirebaseFirestore.instance
            .collection('users')
            .doc(userId)
            .collection("FavouritePlaces")
            .get();
    if (userFavouritePlaces.docs.isNotEmpty) {
      try {
        return userFavouritePlaces.docs
            .map((docs) => UserFavPlaces.fromJson(docs.data()))
            .toList();
      } catch (e) {
        print(e.toString());
      }
    } else {
      return null;
    }
  }

'''