How to show product by category flutter Firestore

Viewed 614

I'm trying to filter my list by category but no success so far. I've tried several ways to implement it and also asked some friends. Unfortunately non of their answers has worked yet.

This is the full code for the screen. I'd like to be able to filter documents by category, something like: "Where()"...

Any idea?

Thanks.

homeService file

import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:tabib/core/view_model/services/home_sevices.dart';
import 'package:tabib/models/category_model.dart';
import 'package:tabib/models/doctor_model.dart';

class HomeViewModel extends GetxController{

  ValueNotifier<bool> get loading => _loading;
  final ValueNotifier<bool> _loading = ValueNotifier(false);

  List<CategoryModel> get categoryModel => _categoryModel;
  final List<CategoryModel> _categoryModel = [];

  List<DoctorsModel> get doctorsyModel => _doctorsyModel;
  final List<DoctorsModel> _doctorsyModel = [];

  HomeViewModel() {
    getCategory();
    getDoctors();
  }
  getCategory() async{
    _loading.value = true;
    HomeServices().getCategory().then((value) {
      for(int i = 0; i< value.length; i++) {
        _categoryModel.add(CategoryModel.fromJson(value[i].data() as Map<dynamic, dynamic>));
        _loading.value = false;
      }
      update();
    });
  }
  getDoctors() async {
    _loading.value = true;
    HomeServices().getDoctors().then((value) {
      for(int i = 0; i< value.length; i++) {
        _doctorsyModel.add(DoctorsModel.fromJson(value[i].data() as Map<dynamic, dynamic>));
        _loading.value = false;
        print(_doctorsyModel);
      }
      update();
    });
  }
}

    import 'package:cloud_firestore/cloud_firestore.dart';
    
    class HomeServices{
      final CollectionReference _categoryColletionRef =
      FirebaseFirestore.instance.collection('categories');
    
      final CollectionReference _doctorsColletionRef =
      FirebaseFirestore.instance.collection('doctors');
    
      Future<List<QueryDocumentSnapshot>> getCategory () async {
        var value = await _categoryColletionRef.get();
    
            return value.docs;
      }
      Future<List<QueryDocumentSnapshot>> getDoctors () async {
        var value = await _doctorsColletionRef.get();
    
        return value.docs;
      }
    }

class DoctorsModel {
  late String name;
  late String phone;
  late String subCat;

  DoctorsModel({required this.name,required this.phone, required this.subCat});

  DoctorsModel.fromJson(Map<dynamic, dynamic> map) {
    if(map == null) {
      return;
    }
    name = map ['name'];
    phone = map ['phone'];
    subCat = map ['subCat'];
  }
  toJson() {
    return {
      'name': name,
      'phone': phone,
      'subCat': subCat,
    };
  }
}

class CategoryModel {
  late String title;
  late String image;
  late int catID;


  CategoryModel ({required this.catID,required this.title, required this.image});

  CategoryModel.fromJson(Map<dynamic, dynamic> map) {
    if (map == null) {
      return;
    }
    catID = map['catID'];
    title = map['title'];
    image = map['image'];

  }

  toJson() {
    return {
      'catID': catID,
      'title': title,
      'image': image,
    };
  }
}
1 Answers

Posting Frank van Puffelen's comment as a Wiki answer for visibility.

You can filter your documents by category using querying methods described in the FlutterFire docs.

For example, the where method:

FirebaseFirestore.instance
  .collection('categories')
  .where()
  .get()
  .then(...);
Related