【问题标题】:How to show product by category flutter Firestore如何按类别显示产品flutter Firestore
【发布时间】:2022-01-04 11:18:17
【问题描述】:

我正在尝试按类别过滤我的列表,但到目前为止没有成功。我尝试了几种方法来实现它,也问了一些朋友。不幸的是,他们的答案都没有奏效。

这是屏幕的完整代码。我希望能够按类别过滤文档,例如:“Where()”...

有什么想法吗?

谢谢。

homeService 文件

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

【问题讨论】:

标签: firebase flutter dart google-cloud-firestore


【解决方案1】:

将 Frank van Puffelen 的评论发布为 Wiki 答案以提高知名度。

您可以使用FlutterFire docs 中描述的查询方法按类别过滤文档。

例如where方法:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-16
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多