I was to trying to get an images from my firestore database. I used streambuilder to get images but it is showing me an error null value received. I checked the code completely and it was perfect. I dont know where the problem exist. Kindly help. Thanks.
Error
Null check operator used on a null value.
The following _CastError was thrown building StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(dirty, state: _StreamBuilderBaseState<QuerySnapshot<Map<String, dynamic>>, AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>>>#b7a15):
GetImage Code
class GetImage extends StatelessWidget {
const GetImage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: FirebaseFirestore.instance
.collection('users')
.doc(FirebaseAuth.instance.currentUser!.uid)
.collection('cart')
.snapshots(),
builder: (context,
AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
return ListView.builder(
itemCount: snapshot.data!.docs.length,
itemBuilder: (context, index) {
return Container(
child: Image.network(
snapshot.data!.docs[index].data()['imageUrl'],
fit: BoxFit.cover,
alignment: Alignment.center,
),
);
});
});
}
}
2
Answers
The error is showing because you have not provided the code for the loading state of a streambuilder, when the data is being loaded. Here is the updated code.
You need to wait to fetch the data