Home > Mobile >  Flutter The operator '[]' isn't defined for the type 'Object'
Flutter The operator '[]' isn't defined for the type 'Object'

Time:10-05

I've searched a lot and try every possible solutions but can't solve my problem.

here is my code

late QuerySnapshot searchSnapshot;

Widget searchList() {
return searchSnapshot != null
    ? ListView.builder(
        itemCount: searchSnapshot.docs.length,
        shrinkWrap: true,
        itemBuilder: (context, index) {
          return SearchList(
            userName: searchSnapshot.docs[index].data()!['name'], //error on ['name']
            userEmail: searchSnapshot.docs[index].data()!['email'], //error on ['email']
          );
        })
    : Container();
}

Error:

The operator '[]' isn't defined for the type 'Object'. Try defining the operator '[]'.

CodePudding user response:

You lose the type of data when you define searchSnapshot.

Try changing it to:

late QuerySnapshot<Map<String, dynamic>> searchSnapshot;

CodePudding user response:

looks like the function data() need return a Map, not Object

  • Related