Error: the operation '[]' is not defined for the type 'object'

232 views Asked by At

I am using Null -Safety then I keep getting this error, anywhere I user snapshot in my code

here is the error

error

here is my code

StreamBuilder(
                          stream: firestore
                              .collection('interest')
                              .doc('${auth.currentUser?.uid}')
                              .snapshots(),
                          builder: (context, snapshot) {
                            if (!snapshot.hasData) {
                              return Center(
                                child: CircularProgressIndicator(),
                              );
                            }
                            return ListView.builder(
                                scrollDirection: Axis.horizontal,
                                itemCount: snapshot.data!['Interest'].length ,
                                itemBuilder: (context, index) {
                                  return Padding(
                                    padding: const EdgeInsets.only(top: 12.0),
                                    child: bottomCardList(
                                      'assets/1 (6).jpeg',
                                      snapshot.data!['Interest'][index]
                                          .toString(),
                                    ),
                                  );
                                });
                          }),

Thanks

2

There are 2 answers

0
CopsOnRoad On

There are a few solutions:

  • Provide a type to your StreamBuilder:

    StreamBuilder<DocumentSnapshot<Map>> (...)
    
  • Provide a type to the second parameter of your builder:

    builder: (context, AsyncSnapshot<Map> snapshot)
    
  • Use as to downcast the Object to Map

    (snapshot.data as Map)['key']
    
0
Gbenga B Ayannuga On

i solve this by using the type

StreamBuilder<DocumentSnapshot<Map>>(
                          stream: firestore
                              .collection('interest')
                              .doc('${auth.currentUser?.uid}')
                              .snapshots(),
                          builder: (context, snapshot) {
                            if (!snapshot.hasData) {
                              return Center(
                                child: CircularProgressIndicator(),
                              );
                            }
return ListView.builder(
                                scrollDirection: Axis.horizontal,
                                itemCount: snapshot.data!['Interest'].length ,
                                itemBuilder: (context, index) {
                                  return Padding(
                                    padding: const EdgeInsets.only(top: 12.0),
                                    child: bottomCardList(
                                      'assets/1 (6).jpeg',
                                      snapshot.data!['Interest'][index]
                                          .toString(),
                                    ),
                                  );
                                });
                          }),