I want to sort the posts in my application by date, but when I use orderby I get a null check operator used on a null value
error.
My code below:
StreamBuilder(
stream: FirebaseFirestore.instance
.collection('posts')
.where('uid', isEqualTo: widget.uid)
.orderBy(
'datePublished',
descending: true,
)
.snapshots(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(),
);
}
return ListView.builder(
primary: false,
shrinkWrap: true,
itemCount: (snapshot.data! as dynamic).docs.length,
itemBuilder: (context, index) {
DocumentSnapshot snap =
(snapshot.data! as dynamic).docs[index];
CodePudding user response:
because stream can be return null value then snapshot.data! as dynamic make error. Please check snapshot.hasData
CodePudding user response:
Add 1 more condition
if (snapshot.data == null) {
return const Center(
child: CircularProgressIndicator(),
);
}