the problem is that I can not Map data from firestore firebase inside "children" of GridView.count. I want to Map a specific document from firebase inside of GridView.count. Please Help me.
productSnap.data.map((document) please help me how to correct this and I am facing this error when I use my code.
Class '_JsonDocumentSnapshot' has no instance method 'map'.
Receiver: Instance of '_JsonDocumentSnapshot'
Tried calling: map(Closure: (dynamic) => ProductCard)
ProductCard is the name of the widget in this code....
FutureBuilder(
future: _firebaseServices.productsRef
.doc(document.id)
.get(),
builder: (context, productSnap) {
if (productSnap.hasError) {
return Container(
child: Center(
child: Text("${productSnap.error}"),
),
);
}
if (productSnap.connectionState ==
ConnectionState.done) {
return GridView.count(
crossAxisCount: 2,
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
childAspectRatio: .53,
mainAxisSpacing: 8.0,
crossAxisSpacing: 10,
padding: EdgeInsets.only(
top: 10.0,
bottom: 12.0,
left: 5,
right: 5),
children:
///////////////// the problem is in here ///////////////
productSnap.data.map((document) {
return ProductCard(
title: document['name'],
);
}).toList());
}
return Container(
child: Center(
child: CircularProgressIndicator(),
),
);
});
CodePudding user response:
You should use querysnapshot here not document, update your future to:-
future:_firebaseServices.productsRef.get(),
builder: (context,AsyncSnapshot<QuerySnapshot> productSnap) {
And gridview children as :-
// ...
children: List.generate(productSnap.data!.docs.length,(index){
var document= productSnap.data!.docs[index];
return ProductCard(
title: document['name'],
);
})
CodePudding user response:
I finally found out how to do it with the help of Deepak Lohmod. I also added "id" field in my Products. id is equal to the document uid of Product.
FutureBuilder(
future: _firebaseServices.productsRef
.where("id", isEqualTo: document.id)
.get(),
builder: (context, productSnap) {
if (productSnap.hasError) {
return Container(
child: Center(
child: Text("${productSnap.error}"),
),
);
}
if (productSnap.connectionState ==
ConnectionState.done) {
return GridView.count(
crossAxisCount: 2,
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
childAspectRatio: .53,
mainAxisSpacing: 8.0,
crossAxisSpacing: 10,
padding: EdgeInsets.only(
top: 10.0,
bottom: 12.0,
left: 5,
right: 5),
children: productSnap.data.docs
.map<Widget>((document) {
final dynamic data = document.data();
return ProductCard(
title: data['name'],
);
}).toList());
}
return Container(
child: Center(
child: CircularProgressIndicator(),
),
);
});