How to get favourite data from firestore using flutter

Viewed 119

I have created the below method that make the item favourite as favourite like the below one:

  void petFavorite(String petId) {
    FirebaseFirestore.instance
        .collection('pets')
        .doc(petId)
        .collection('favorites')
        .doc(userModel.uId)
        .set({
      'favorite': true,
    }).then((value) {
      emit(AppGetFavoritesSuccessState());
    }).catchError((error) {
      emit(AppGetFavoritesErrorState(error.toString()));
    });
  }

and this the below method which gets all the data in the fire Store:

List<String> petsId = [];
List<PetsModel> pets = [];

  void getPetsData() {
    FirebaseFirestore.instance.collection('pets').get().then((value) {
      value.docs.forEach((element) {
        petsId.add(element.id);
        pets.add(PetsModel.fromJson(element.data()));
      });
      emit(AppGetPetsSuccessState());
    }).catchError((error) {
      emit(AppGetPetsErrorState(error.toString()));
    });
  }

and this is the below models.. Pet Model:

class PetsModel {
  String image;
  String name;
  String gender;
  String age;
  String petType;
  String price;
  String type;
  String distance;

  PetsModel({
    this.image,
    this.name,
    this.gender,
    this.petType,
    this.price,
    this.age,
    this.type,
    this.distance,
  });

  PetsModel.fromJson(Map<String, dynamic> map) {
    image = map['image'];
    name = map['name'];
    gender = map['gender'];
    age = map['age'];
    petType = map['animal_type'];
    price = map['price'];
    type = map['type'];
    distance = map['distance'];
  }

  Map<String, dynamic> toMap() {
    return {
      'name': name,
      'image': image,
      'gender': gender,
      'animal_type': petType,
      'age': age,
      'price': price,
      'type': type,
      'distance': distance,
    };
  }
}

user model:

class UserModel {
  String name;
  String email;
  String phone;
  String uId;
  String image;
  String cover;
  String bio;
  bool isEmailVerified;

  UserModel({
    this.name,
    this.email,
    this.phone,
    this.uId,
    this.image,
    this.cover,
    this.bio,
    this.isEmailVerified,
  });

  UserModel.fromJson(Map<String, dynamic> json) {
    email = json['email'];
    name = json['name'];
    phone = json['phone'];
    uId = json['uId'];
    image = json['image'];
    cover = json['cover'];
    bio = json['bio'];
    isEmailVerified = json['isEmailVerified'];
  }

  Map<String, dynamic> toMap() {
    return {
      'name': name,
      'email': email,
      'phone': phone,
      'uId': uId,
      'image': image,
      'cover': cover,
      'bio': bio,
      'isEmailVerified': isEmailVerified,
    };
  }
}

I tried the below code but it brings all pets:

  List<PetsModel> favoritePets = [];
  void getFavoritePets() {
    emit(AppGetFavoritePetsLoadingState());
    FirebaseFirestore.instance.collection('pets').get().then((value) {
      value.docs.forEach((element) {
        element.reference.collection('favorites').where('favorite', isEqualTo: true).get().then((value) {
          favoritePets.add(PetsModel.fromJson(element.data()));
        });
      });
    });
  }

and this method get the userData:

UserModel userModel;
  getUserData() {
    emit(AppGetUserLoadingState());
    FirebaseFirestore.instance.collection('users').doc(uId).get().then((value) {
      userModel = UserModel.fromJson(value.data());
      emit(AppGetUserSuccessState());
    }).catchError((error) {
      print(error.toString());
      emit(AppGetUserErrorState(error.toString()));
    });
  }

I need only the favourite pets for every own user..

Is this the right logic I am working on? or I have to save this favourites collection in every users instead every pet?

so now I want to create a method that gets the favourites from the below collection as it looks like the below image:

enter image description here

and this is the below user data:

enter image description here

1 Answers

In order to filter your data, try to run the query (.where()) just before the .get() method. Just wanted to point out to avoid sharing private information in your posts.

Related