0
votes

I get the following error "The getter 'length' isn't defined for the type 'Object'" when checking the length of snapshot.data in the itemCount property of a listViewBuilder:

child: StreamBuilder(
       stream:_firestoreService?.getProducts(), 
             builder: (context, snapshot) {
                   if (!snapshot.hasData) {
                          return CircularProgressIndicator();
                   } else {
                          return ListView.builder(
                                 itemExtent: 80,
                                 itemCount: snapshot.data!.length,
                                 itemBuilder: (context, index) {

The data is coming from firestore

2

2 Answers

0
votes

In the latest versions of the cloud_firestore plugin, you need to indicate the type of your query, before you can get the data from it. I recommend checking out the documentation on migrating to version 2.0, which shows that this typically will look something like changing

Query query

To

Query<Map<String, dynamic>> query

The exact code you need to change depends on your implementation, and isn't clear from what you shared, but it'll be some type of type annotation like shown above (and in the docs I linked).

0
votes

For any others who come across this problem.

Thanks to @Frank van Puffelen's answer above that states: "you need to indicate the type of your query before you can get the data from it".

In my case, the type of data I was expecting was: DocumentSnapshot<Map<String, dynamic>>>. After this, use the code below:

snapshot.data!.data()!.length,