简体   繁体   中英

Flutter Search Delegate with Firestore

I m implementing a search feature using Flutter Search Delegate and the data is stored in Firestore. I can't figure out why this error is coming up.

Widget buildSuggestions(BuildContext context) {
  return StreamBuilder(
    stream: Firestore.instance.collection('todos').snapshots(),
    builder: (context, snapshot) {
      if (!snapshot.hasData) return new Text('Loading...');

      final results =
          snapshot.data.documents.where((a) => a['title'].contains(query));

      return ListView(
        children: results.map<Widget>((a) => Text(a['title'])).toList(),
      );
    },
  );
}

Error:

type '(dynamic) => dynamic' is not a subtype of type '(DocumentSnapshot) => bool' of 'test'

Replace the line

final results = snapshot.data.documents.where((a) => a['title'].contains(query));

to

final results = snapshot.data.documents.where((DocumentSnapshot a) => a.data['title'].contains(query));

And the line

children: results.map<Widget>((a) => Text(a['title'])).toList()

to

children: results.map<Widget>((a) => Text(a.data['title'])).toList()

Replace the Line

builder: (context, snapshot) {
  ....

With

builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
.....

Final code be:

 Widget buildSuggestions(BuildContext context) {
    return StreamBuilder(
      stream: FirebaseFirestore.instance.collection('todos').snapshots(),
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (!snapshot.hasData) return new Text('Loading...');

        final results =
            snapshot.data.docs.where((a) => a['title'].contains(query));

        return ListView(
          children: results.map<Widget>((a) => Text(a['title'])).toList(),
        );
      },
    );
  }
       

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