使用搜索栏过滤时 Firestore 查询不返回结果

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

我正在构建一个 Flutter 应用程序,使用 Firestore 作为后端数据库。我有一系列产品,我正在尝试实现一个搜索功能,该功能根据搜索文本过滤产品,并且搜索完全通用,因为无论产品类别如何,它都可以提供搜索结果

class ProductGrid extends StatefulWidget {
  final String selectedCategory;
  final String searchText;

  const ProductGrid(
      {Key? key, required this.selectedCategory, required this.searchText})
      : super(key: key);

  @override
  State<ProductGrid> createState() => _ProductGridState();
}

class _ProductGridState extends State<ProductGrid> {
  @override
  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return Expanded(
      child: StreamBuilder<QuerySnapshot>(
        stream: FirebaseFirestore.instance
            .collection('users')
            .doc(FirebaseAuth.instance.currentUser!.uid)
            .collection('products')
            .where('selectedCategory',
                isEqualTo: widget.selectedCategory.isNotEmpty
                    ? widget.selectedCategory
                    : null)
            .where('title', // Filter products by title based on searchText
                isGreaterThanOrEqualTo: widget.searchText.toLowerCase(),
                isLessThanOrEqualTo: widget.searchText.toLowerCase() + 'z')
            .snapshots(),
        builder: (context, snapshot) {
          print('this: ${widget.searchText}');
          print('$snapshot');
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const Center(
                child: SpinKitRing(color: primaryBlue, size: 30));
          } else if (snapshot.hasData) {
            List<Map<String, dynamic>> productsList = snapshot.data!.docs
                .map((doc) => doc.data() as Map<String, dynamic>)
                .toList();
            return GridView.builder(
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 2,
                childAspectRatio: 0.88,
                mainAxisSpacing: size.height * 0.01,
                crossAxisSpacing: size.width * 0.017,
              ),
              itemCount: productsList.length,
              itemBuilder: (context, index) {
                Map<String, dynamic> productData = productsList[index];
                return Container(
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(20),
                    color: Colors.white,
                  ),
                  child: //rest of the code
                          ),
                          child: //rest of the code
                                  ),
                                );
                              },
                              child: Image.asset(
                                'assets/images/iphoneImage.png',
                              ),
                            ),
                          ),
                        ),
                        SizedBox(height: size.height * 0.015),
                        Padding(
                          padding: const EdgeInsets.only(left: 12),
                          child: Row(
                            children: [
                              Text(
                                productData['title'],
                                style: customTextblack,
                              ),
                              //remaining code fetching titles and products from databse
                            ],
                          ),
                        ),
                        //remaining code
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                );
              },
            );
          } else {
            return CircularProgressIndicator();
          }
        },
      ),
    );
  }
}

但是,当我在应用程序中输入搜索查询时,该查询不会返回任何结果,即使我有与搜索文本匹配的产品。

我已确保搜索文本与我的数据库中某些产品的标题相匹配,并且类别过滤似乎工作正常。

有人可以帮助我确定为什么 Firestore 查询没有返回任何结果,即使数据库中应该有匹配的产品?任何见解或建议将不胜感激。谢谢!

我的应用程序条形码以防万一您想查看它

class AppSearchBar extends StatelessWidget {
  final ValueChanged<String> onChanged;
  final String hintText;
  const AppSearchBar(
      {super.key, required this.hintText, required this.onChanged});

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return Container(
      height: size.height * 0.06,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(30),
        color: Colors.white,
      ),
      child: Center(
        child: TextFormField(
            onChanged: onChanged,
            decoration: InputDecoration(
                contentPadding: const EdgeInsets.all(12.0),
                prefixIcon: Padding(
                  padding: const EdgeInsets.all(10.0),
                  child: SvgPicture.asset(
                    'assets/images/magnifyingglass.svg',
                  ),
                ),
                iconColor: const Color(0xffC2C2C2),
                border: InputBorder.none,
                hintText: hintText,
                hintStyle: lowOpacityText)),
      ),
    );
  }
}
flutter firebase dart google-cloud-firestore
1个回答
0
投票

在你的冷杉采石场中像这样改变

FirebaseFirestore.instance
        .collection('users')
        .doc(FirebaseAuth.instance.currentUser!.uid)
        .collection('products')
        .where('selectedCategory',
            isEqualTo: widget.selectedCategory.isNotEmpty
                ? widget.selectedCategory
                : null)
        //change this
        .orderBy('title').
        .startAt([widget.searchText.toLowerCase()])    
        .snapshots(),
© www.soinside.com 2019 - 2024. All rights reserved.