简体   繁体   中英

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

I have some problems with buildSuggestions. When I type something, it shows "Instance of 'DocumentSnapshot' " instead of the name of documents, like this image below.

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.

What you have to do is use the docs property of that QuerySnapshot object, which is exactly what you want, a 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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