Home > Software engineering >  filtering data from firebase flutter
filtering data from firebase flutter

Time:12-28

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

CodePudding user response:

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;
    }
  }

'''

  • Related