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

5,977

According to the cloud_firestore plugin github:

/// A [DocumentSnapshot] contains data read from a document in your [FirebaseFirestore]
/// database.
///
/// The data can be extracted with the data property or by using subscript
/// syntax to access a specific field.

So perhaps this modification to your code would work:

List<Brew> _brewListFromSnapshot(QuerySnapshot snapshot) {
    return snapshot.docs.map((document) {
      return Brew(
        name: document['name'] ?? '',
        strength: document['strength'] ?? 0,
        sugars: document['sugars'] ?? '0',
      );
    }).toList();
  }
Share:
5,977
P A T Himaranga
Author by

P A T Himaranga

Updated on December 29, 2022

Comments

  • P A T Himaranga
    P A T Himaranga over 1 year

    I'm trying to get documents in brews collection from Firestore.

    What is wrong with my code?

    Help me somebody who is used to Flutter and Firebase.

    ////brew list from snapshot
    List<Brew> _brewListFromSnapshot(QuerySnapshot snapshot) {
        return snapshot.docs.map((document) {
          return Brew(
            name: document.data()['name'] ?? '',
            strenght: document.data()['strength'] ?? 0,
            sugars: document.data()['sugars'] ?? '0',
          );
        }).toList();
      }
    

    Attached screenshots regarding error: