Daniel loaiza
Daniel loaiza

Reputation: 125

Class 'AsyncSnapshot<QuerySnapshot>' has no instance getter 'docs'

i'm chargin data from firestore and i have this, I'm new in firestore and I don't know so much, so ¿Can you help me?

Container(
  height: 700,
  child: GridView.builder(
    itemCount: snapShot.data.docs.length,
    // ignore: non_constant_identifier_names
    itemBuilder: (context, index) => SingleProduct(
        name: snapShot.docs.data[index]["name"],
        price: snapShot.docs.data[index]["precio"],
        image: snapShot.docs.data[index]["image"]),
    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 2,
        childAspectRatio: 0.7,
        crossAxisSpacing: 10,
        mainAxisSpacing: 10),
  ),
)

And the error is this: Class 'AsyncSnapshot' has no instance getter 'docs'. Receiver: Instance of 'AsyncSnapshot' Tried calling: docs

Upvotes: 0

Views: 59

Answers (1)

creativecreatorormaybenot
creativecreatorormaybenot

Reputation: 126624

The problem here is that you are mistaken one snapshot with the other.

You have an instance of AsyncSnapshot in your snapShot variable. This snapshot might be in a loading state or contain the snapshot you are looking for.

You can check if it already has data like this:

final querySnapshot = snapshot.data;
if (querySnapshot == null) return Container();

// Now, you are safe to use your querySnapshot.
querySnapshot.docs
// ...

The error in your code are the following lines:

itemBuilder: (context, index) => SingleProduct(
                    name: snapShot.docs.data[index]["name"],
                    price: snapShot.docs.data[index]["precio"],
                    image: snapShot.docs.data[index]["image"]),

Here you are trying to access snapShot.docs instead of the correct snapShot.data.docs.

Upvotes: 2

Related Questions