how to get images from firestore to gridview - flutter

am trying to fetch image from firestore to gridview.but to each container where image should be displayed am getting the error.....NoSuchMethodError: '[]',Dynamic call of null,Receiver:instanceof '_jsonQuerySnapshot', argument[0]..
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection("Products")
.snapshots(),
builder:
(BuildContext context, AsyncSnapshot snapshot) {
return snapshot.data == null
? const Center(
child: CircularProgressIndicator())
: GridView.builder(
gridDelegate:
const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2),
itemBuilder:
(BuildContext context, int index) {
DocumentSnapshot data =
snapshot.data![index];
Map getdoc =
data.data() as Map<String, dynamic>;
return Container(
height: height * 0.3,
width: width * 0.3,
child: Image.network(
getdoc["product_image_url"]
[index]),
);
},
itemCount: snapshot.data!.docs.length,
);
}))

Try this.
child: Image.network(getdoc["product_image_url"]),

Related

How to listen to stream and update Flutter GridView builder accessing Firebase

I need an example code in which a Flutter Gridview builder streams Firebase data (for example with a single Collection and multiple Documents with multiple fields in each) that can be modified by the user straight from the Gridview
I've watched this tutorial => https://www.youtube.com/watch?v=ErP_xomHKTw
Although it shows how to access and modify data from Firebase I still can't wrap my head around the Firebase and Gridview builder interaction.
Stream Builder using GridView
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection('products').snapshots(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return GridView.builder(
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 200,
childAspectRatio: 3 / 2,
crossAxisSpacing: 20,
mainAxisSpacing: 20),
itemCount: snapshot.data!.docs.length,
itemBuilder: (context, index) {
DocumentSnapshot doc = snapshot.data!.docs[index];
return Column(
children:[
Text(doc['name']),
Text(doc['price']);
);
);
}),
} else {
return Text("No data");
}
},
)
Stream Builder using ListView
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection('products').snapshots(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data!.docs.length,
itemBuilder: (context, index) {
DocumentSnapshot doc = snapshot.data!.docs[index];
return Column(
children:[
Text(doc['name']),
Text(doc['price']);
);
});
} else {
return Text("No data");
}
},
)

Error: the operation '[]' is not defined for the type 'object'

I am using Null -Safety then I keep getting this error, anywhere I user snapshot in my code
here is the error
here is my code
StreamBuilder(
stream: firestore
.collection('interest')
.doc('${auth.currentUser?.uid}')
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(),
);
}
return ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: snapshot.data!['Interest'].length ,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.only(top: 12.0),
child: bottomCardList(
'assets/1 (6).jpeg',
snapshot.data!['Interest'][index]
.toString(),
),
);
});
}),
Thanks
i solve this by using the type
StreamBuilder<DocumentSnapshot<Map>>(
stream: firestore
.collection('interest')
.doc('${auth.currentUser?.uid}')
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(),
);
}
return ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: snapshot.data!['Interest'].length ,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.only(top: 12.0),
child: bottomCardList(
'assets/1 (6).jpeg',
snapshot.data!['Interest'][index]
.toString(),
),
);
});
}),
There are a few solutions:
Provide a type to your StreamBuilder:
StreamBuilder<DocumentSnapshot<Map>> (...)
Provide a type to the second parameter of your builder:
builder: (context, AsyncSnapshot<Map> snapshot)
Use as to downcast the Object to Map
(snapshot.data as Map)['key']

Why GridView.builder clear when obtain more data in flutter

I have my GRidView builder and I have my future and my ScrollController, why my gridView clear when scrollController is equal than mi maxScroll and obtain more information? It's supposed to show all of it, not the latest information.
this is my code
future: _future ,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if(snapshot.hasData){
List<dynamic> loaded = snapshot.data;
return
Container(
width: double.infinity,
height: _screenSize.height * .8,
child: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
childAspectRatio: MediaQuery.of(context).size.width /(MediaQuery.of(context).size.height /1.5),),
controller: _controller,
itemCount: loaded.length,
itemBuilder: (context, i){
return _row(context,loaded,i);
},
)
);
}else{
and the constructor
Future<List> _future;
ScrollController _controller = ScrollController(initialScrollOffset: 0.0, keepScrollOffset: true);
_SearchPageState(){
_controller.addListener(() {
if(_controller.position.pixels >= _controller.position.maxScrollExtent-30 ){
setState(() {
_future = videoJuegos.getPaginationPopular();
});
}
});
_future = videoJuegos.getPaginationPopular();
}
Your Gridview.builder is clearing because of this line:
List<dynamic> loaded = snapshot.data;
It will clear the current list and add the new data. You should use initialize your list above the future and then use list.add:
List<dynamic> loaded = [];
future: _future ,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if(snapshot.hasData){
loaded.add(snapshot.data);
return
Container(
width: double.infinity,
height: _screenSize.height * .8,
child: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
childAspectRatio: MediaQuery.of(context).size.width /(MediaQuery.of(context).size.height /1.5),),
controller: _controller,
itemCount: loaded.length,
itemBuilder: (context, i){
return _row(context,loaded,i);
},
)
);
}else{
Hope that helps.

How to increment a list variable based on item index?

im getting an error: type int is not a subtype of type List<dynamic> of function result on pressing add
final itemcount = [];
Widget buildItem(BuildContext context, DocumentSnapshot document, int index) {
return Container(
child: Flatbutton(
child:Text("add"),
onpressed:(){
itemcount[index]=itemcount[index]+1,
}
)
);
}
StreamBuilder<QuerySnapshot>(
stream: Firestore.instance
.collection('products')
.where('category', isEqualTo: 'ourstore')
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: Icon(Icons.photo_library),
);
} else {
return snapshot.data.documents.isEmpty
? Center(child: null)
: GridView.builder(
itemBuilder: (context, index) => buildItem(
context,
snapshot.data.documents[index],
index),
itemCount: snapshot.data.documents.length,
physics: NeverScrollableScrollPhysics(),
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(
// childAspectRatio: 3/4,
crossAxisSpacing: 10,
mainAxisSpacing: 5,
crossAxisCount: 2),
shrinkWrap: true,
scrollDirection: Axis.vertical,
);
}
},
),
fixed the error
had to set the value to zero for every item in the list
code below:
List<int> itemcount = List<int>();
Widget buildItem(
BuildContext context, DocumentSnapshot document, int index) {
itemcount.add(0);
return Container(
child: Flatbutton(
child:Text("add"),
onpressed:(){
setState(() {
itemcount[index]++;
});
}
)
);
}

FutureBuilder inside ListView.builder not working

Want to display items using FutureBuilder inside a ListView.builder. However, theres no display. help pls
body: ListView.builder(
itemExtent: 25.0,
itemCount: _posts.length,
itemBuilder: (BuildContext context, int index){
Post post = _posts[index];
return FutureBuilder(
future: DatabaseService.getUserWithId(post.authorId),
builder: (BuildContext context, AsyncSnapshot snapshot) {
return Container(
height: 200.0,
margin: EdgeInsets.all(10.0),
color: Colors.red,
);
},
);
},
),