Home > front end >  The operator '[]' isn't defined for the type 'Object' for DocumentSnapshot
The operator '[]' isn't defined for the type 'Object' for DocumentSnapshot

Time:03-02

In my application I use the following code to retrieve fields of a collection of a document from Firebase Firestore Database:

var animalList = <Animal>[];
  documents.forEach((d) {
    animalList.add(Animal(
        d.data()['animal']['name'],
        d.data()['animal']['weight']));
  });

The document in the database looks like this:

enter image description here

I get the error "The operator '[]' isn't defined for the type 'Object'". But this code already worked in the past with older Firebase versions.

How can I solve this issue?

CodePudding user response:

add AsyncSnapshot before variable d

FutureBuilder(
  future: ...,
  builder: (context,AsyncSnapshot d) {
  ....
  }
);

CodePudding user response:

I solved it with the following code:

var animalList = <Animal>[];
  documents.forEach((d) {
    animalList.add(Animal(
       d['animal']['name'],
       d['animal']['weight']));
  });
  • Related