简体   繁体   English

Flutter - 使用 Firestore 实例的 BuildSuggestions(搜索代理)出错

[英]Flutter - Error in BuildSuggestions (Search Delegate) using Firestore instance

I have some problems with buildSuggestions.我对 buildSuggestions 有一些疑问。 When I type something, it shows "Instance of 'DocumentSnapshot' " instead of the name of documents, like this image below.当我键入内容时,它显示“Instance of 'DocumentSnapshot'”而不是文档名称,如下图所示。

Here is my code:这是我的代码:


@override
Widget buildSuggestions(BuildContext context) {


  return StreamBuilder(

    stream: Firestore.instance.collection("anuncios").where("caseSearch", arrayContains: query).snapshots(),
    builder: (context, snapshot) {
      if (query.isNotEmpty);

      List<DocumentSnapshot> results = snapshot.data.documents.where(
              (DocumentSnapshot a) => a.data["titulo"].toString().contains(query)).toList();

      return ListView.builder(
          itemCount: results.length,
          itemBuilder: (context, index){


            return ListTile(
              onTap: (){
                close(context, results[index].toString());
              },
              title: Text(results[index].toString()),
            );
          }
      );

    },

  );
}

在此处输入图像描述

The problem with your code is that your snapshot.data.documents.where(...) returns a QuerySnapshot object, which cannot be converted to a list of DocumentSnapshot by doing a .ToList() after it.您的代码的问题是您的snapshot.data.documents.where(...)返回一个QuerySnapshot object,无法通过在其后执行.ToList()将其转换为DocumentSnapshot列表。

What you have to do is use the docs property of that QuerySnapshot object, which is exactly what you want, a List<DocumentSnapshot> .您需要做的是使用QuerySnapshot object 的docs属性,这正是您想要的,一个List<DocumentSnapshot> So applyin that to your code, you just have to replace the mentioned line for this:因此,将其应用于您的代码,您只需为此替换上述行:

List<DocumentSnapshot> results = (snapshot.data.documents.where((DocumentSnapshot a) => a.data["titulo"].toString().contains(query))).docs;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM