Mt Khalifa
Mt Khalifa

Reputation: 498

Why is Firestore data giving Class 'QueryDocumentSnapshot' has no instance method '[]'. error?

I am trying to get the image from firestore and it give this error, the key to the image in database is 'image 1 Url'

the same Code and same structure works on my other app but this one it throws this error

Class 'QueryDocumentSnapshot' has no instance method '[]'.
Receiver: Instance of 'QueryDocumentSnapshot'
Tried calling: []("image 1 Url")

this is the firestore database structure, enter image description here

class Toyota extends StatefulWidget {
  @override
  _ToyotaState createState() => _ToyotaState();
}

class _ToyotaState extends State<Toyota> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: StreamBuilder(
          stream: FirebaseFirestore.instance.collection('toyota').snapshots(),
          builder: (context, snapshot) {
            if (snapshot.data == null)
              return Center(
                child: CircularProgressIndicator(
                  backgroundColor: Colors.red,
                  valueColor: new AlwaysStoppedAnimation<Color>(Colors.teal),
                ),
              );
            return ListView.builder(
                itemCount: snapshot.data.documents.length,
                itemBuilder: (context, index) => SingleChildScrollView(
                      child: Padding(
                        padding: const EdgeInsets.all(15.0),
                        child: Container(
                              decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(3)),
                          child: Image.network(
                            snapshot.data.documents[index]['image 1 Url'],
                            ),
                        ),
                      ),
                    ));
          },
        ));
  }
}

Upvotes: 0

Views: 1381

Answers (1)

ElsayedDev
ElsayedDev

Reputation: 650

the solution I found to use get('String') this work with me

snapshot.data.documents[index].get('image 1 Url')

you can check this for more : https://developers.google.com/android/reference/com/google/firebase/firestore/QueryDocumentSnapshot

Upvotes: 7

Related Questions