firebase mapping error for the flutter project

Viewed 713
Widget searchList() {
 return ListView.builder(
 itemCount: searchSnapshot.docs.length,
   itemBuilder: (context, index) {
     return SearchTile(
     userName: searchSnapshot.docs[index].data["name"],
     userEmail: searchSnapshot.docs[index].data["email"],
     );
     });
  }

The operator '[]' isn't defined for the type 'Map<String, dynamic> Function()'. Try defining the operator '[]'.

I am getting this error Please help me with that

2 Answers

I got the same error after updating firebase related packages of my project right now.

Simple fix is to change

searchSnapshot.docs[index].data["name"]

to

searchSnapshot.docs[index].data()["name"]

so to add the () behind data.

It seams like google changed the syntax to fit better together with the admin sdk in firebase cloud functions or any other backend.

Source: https://github.com/FirebaseExtended/flutterfire/blob/master/packages/cloud_firestore/cloud_firestore/lib/src/document_snapshot.dart#L38 / https://stackoverflow.com/a/63529675/3917673

Do this,


Widget searchList() {
 return ListView.builder(
 itemCount: searchSnapshot.docs.length,
   itemBuilder: (context, index) {
     return SearchTile(
     userName: searchSnapshot.docs[index].data()["name"],
     userEmail: searchSnapshot.docs[index].data()["email"],
     );
     });
  }

Calling [] has been deprecated, now, you'll have to use .data() method.

Related