firebase Flutter arrayContainsAny search导致返回重复文档,是我查询错误还是没有正确更新状态?

yxyvkwin  于 2022-11-25  发布在  Flutter
关注(0)|答案(1)|浏览(57)

下面是我的代码:我成功地获得了搜索结果,但列表中添加了重复的文档/产品。我不确定发生这种情况的确切原因。如果您能提供帮助,我将不胜感激。我正在搜索一个数组,以便在我的应用程序中执行“惰性搜索”类型的功能,如何删除重复项?

List<Map<String, dynamic>> products = [];
  bool searching = false;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Expanded(
          child: !searching
              ? Container()
              : ListView.builder(
                  itemCount: products.length,
                  itemBuilder: ((context, index) {
                    return ProductWidget(
                        price: products[index]['price'],
                        name: products[index]['name'],
                        uid: products[index]['uid'],
                        image: products[index]['image'],
                        description: products[index]['description'],
                        title: products[index]['title'],
                        purchase: false,
                        showDescription: false,
                        doListView: false,
                        id: products[index]['id']);
                  })),
        ),
        Container(
          width: MediaQuery.of(context).size.width,
          color: Colors.white,
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Row(
              children: [
                const Icon(Icons.search),
                Expanded(
                  child: TextField(
                    style: const TextStyle(color: Colors.black),
                    onChanged: (val) {
                      _initiateSearch(val);
                    },
                    decoration: const InputDecoration(
                        hintStyle: TextStyle(color: Colors.black),
                        border: InputBorder.none,
                        hintText: 'Search products'),
                  ),
                ),
              ],
            ),
          ),
        )
      ],
    );
  }

  _initiateSearch(String val) async {
    try {
      if (val.length == 0) {
        setState(() {
          searching = false;
          products.clear();
        });
      } else {
        setState(() {
          searching = true;
          products.clear();
        });

        List array = val.split('');

        var items = await FirebaseFirestore.instance
            .collection('products')
            .where('search_array', arrayContainsAny: array)
            .orderBy('timestamp', descending: false)
            .limit(10)
            .get();

        for (int i = 0; i < items.docs.length; i++) {
          products.add({
            'price': items.docs[i].data()['price'],
            'name': items.docs[i].data()['name'],
            'uid': items.docs[i].data()['uid'],
            'image': items.docs[i].data()['image'],
            'description': items.docs[i].data()['description'],
            'title': items.docs[i].data()['title'],
            'id': items.docs[i].id
          });
        }

        setState(() {});
      }
    } catch (e) {}
  }
vaqhlq81

vaqhlq811#

清除搜索列表,然后添加项目。

_initiateSearch(...){
  ....
  products.clear();
   for (int i = 0; i < items.docs.length; i++) {
          products.add({
  ....

一个更好的选择是返回list然后使用它,而检测products的用例可能很棘手。

相关问题