Why I cannot use where & orderby from FirebaseFirestore in flutter
in my code
StreamBuilder(
stream: FirebaseFirestore.instance
.collection('Posts')
.where('imageType', isEqualTo: 'image')
.orderBy('time', descending: true)
.snapshots(),
I will got error.
itemCount: snapshot.data!.docs.length
Error code
Exception has occurred.
_CastError (Null check operator used on a null value)
Where has problem?
CodePudding user response:
Data can be null, so using your snapshot.data! bang operator will give you that error. By using ! you basically tell the compiler that you promise the value will not be null at that point. However, it is in your case :)
Consider implementing some sort of loading indicator that is returned instead of whatever you are building when you got actual data :)
The where and orderBy have nothing to do with your issue btw.
CodePudding user response:
You can use something like a loading indicator to wait until data comes so it will not be null
For example in your stream builder you can use something like
if (!snapshot.hasData)
return Center(
child: CircularProgressIndicator());
return TheWidgetYouWantToBuildWhenDataComes();