Solved: Can not get data from reference field in firebase (flutter)

Question

Asked by Andrii P on January 08, 2022 (source).

I am trying to get data via reference field.

My code

DocumentReference docRef = FirebaseFirestore.instance
                        .doc(snapshot.data!.data()!["organisator"]);
                    docRef.get().then((DocumentSnapshot document) {
                      if (document.exists) {
                        print(document.data());
                      }
                    });

Print gives me this: {image: img_url, name: Dan Brown}

But when I try to get image or name like this document.data()!["image"] I always get an error:

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

I saw possible solution with document.data.data() but it returns me just an error.

So how can I actually get name or image properties from my response?

Answer

Question answered by Nathaniel A (source).

You need to cast the data to a map like this

document.data() as Map<String, dynamic>
FIREBASE FLUTTER GOOGLE-CLOUD-FIRESTORE
SHARE: