如何在我的 Flutter 应用程序中使用 Firebase 按多个过滤器进行搜索?

问题描述 投票:0回答:1

我为我的 Flutter 应用程序创建了一个搜索栏,并在该搜索栏旁边放置了一个过滤按钮。

当用户单击此过滤按钮时,屏幕上将打开一个“底页”,用户可以选择多个过滤器(参考图像)并应用

我只想在屏幕上显示与这些过滤器匹配的广告。在打开的底部表单上,用户应该能够查看带有图像中字段(类型、年龄、性别等)的广告列表。如果我只使用类型作为范围过滤器选项和分页,那就很容易了。但有多个范围过滤器,我不知道如何做到这一点。

如何组合这些多个查询?为了用户体验,我希望用户使用多个过滤器快速找到符合他们心目中的标准的列表。

enter image description here

我的服务类名为 PetAdoptionService

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:my_pet_app/core/models/animals/pet_adoption_model.dart';
import 'package:my_pet_app/core/services/animals/pet_service.dart';

class PetAdoptionService extends PetService<PetAdoption> {
  final CollectionReference _petsRef =
      FirebaseFirestore.instance.collection('petAdoptions');

  Future<List<PetAdoption>> getPetsByName(String name) async {
    final querySnapshot = await _petsRef.get();
    final pets = <PetAdoption>[];

    for (final doc in querySnapshot.docs) {
      final data = doc.data()! as Map<String, dynamic>;
      final petName = data['name'] as String;

      if (petName.toLowerCase().contains(name.toLowerCase())) {
        pets.add(PetAdoption.fromMap(data));
      }
    }
    return pets;
  }

  // The function that will search with more than one query should be defined here.
}

flutter firebase google-cloud-firestore searchbar searchfiltercollection
1个回答
0
投票

我设法解决了我的问题。由于我认为将来其他朋友也会遇到与我类似的问题,因此我在下面给出了正确的代码,以造福这些朋友。希望对大家有用。

class PetAdoptionService extends PetService<PetAdoption> {
  final CollectionReference _petsRef =
      FirebaseFirestore.instance.collection('petAdoptions');

  // Function that allows a single query
  Future<List<PetAdoption>> getPetsByName(String name) async {
    final querySnapshot = await _petsRef.get();
    final pets = <PetAdoption>[];

    for (final doc in querySnapshot.docs) {
      final data = doc.data()! as Map<String, dynamic>;
      final petName = data['name'] as String;

      if (petName.toLowerCase().contains(name.toLowerCase())) {
        pets.add(PetAdoption.fromMap(data));
      }
    }
    return pets;
  }

  // Function that allows multiple queries
  Future<List<PetAdoption>> getFilteredPets({
    String? type,
    List<String>? breeds,
    String? gender,
    bool? isVaccinated,
    bool? isNeutered,
    String? city,
    String? district,
  }) async {
    Query query = _petsRef;

    if (type != null) {
      query = query.where('type', isEqualTo: type);
    }
    if (breeds != null && breeds.isNotEmpty) {
      query = query.where('breed', whereIn: breeds);
    }
    if (gender != null) {
      query = query.where('gender', isEqualTo: gender);
    }
    if (isVaccinated != null) {
      query = query.where('isVaccinated', isEqualTo: isVaccinated);
    }
    if (isNeutered != null) {
      query = query.where('isNeutered', isEqualTo: isNeutered);
    }
    if (city != null) {
      query = query.where('city', isEqualTo: city);
    }
    if (district != null) {
      query = query.where('district', isEqualTo: district);
    }

    final querySnapshot = await query.get();
    return querySnapshot.docs
        .map((doc) => PetAdoption.fromMap(doc.data() as Map<String, dynamic>))
        .toList();
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.