user18124547
user18124547

Reputation:

How can use firestore using where and orderby in flutter?

How can use firestore using where and orderby in flutter?

When I wanna to use both, I got the error.

enter image description here

Exception has occurred.
_CastError (Null check operator used on a null value)

code

StreamBuilder(
        stream: FirebaseFirestore.instance
            .collection('Posts')
            .where('country', isEqualTo: user.country)
            //.where('country', isEqualTo: 'Australia')
            .orderBy('time', descending: true)
            .snapshots(),
        builder: (context,
            AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const Center(
              child: CircularProgressIndicator(),
            );
          }
          //print(user.country);
          return ListView.builder(
            itemCount: snapshot.data!.docs.length,
            itemBuilder: (context, index) => Container(
              margin: EdgeInsets.symmetric(
                  horizontal: width > webScreenSize ? width * 0.3 : 0,
                  vertical: width > webScreenSize ? 15 : 0),
              child: PostCard(
                snap: snapshot.data!.docs[index],
              ),
            ),
          );
        },

So, why I could not got the data? I'd got the user.value.

Upvotes: 0

Views: 73

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599926

The problem is that you're telling Flutter that snapshot.data is guaranteed to have a value, when in reality it doesn't here:

return ListView.builder(
  itemCount: snapshot.data!.docs.length,

I recommend reading the documentation for StreamBuilder again, as there are many more states than snapshot.connectionState == ConnectionState.waiting that you need to handle.

For example, there could be an error, or there could be no data for another reason:

StreamBuilder(
  stream: FirebaseFirestore.instance
      .collection('Posts')
      .where('country', isEqualTo: user.country)
      .orderBy('time', descending: true)
      .snapshots(),
  builder: (context, AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
    if (snapshot.connectionState == ConnectionState.waiting) {
      return const Center(
        child: CircularProgressIndicator(),
      );
    }

    // 👇 Handle error
    if (snapshot.hasError) {
      return const Center(
        child: Text(snapshot.error),
      );
    }

    // 👇 Handle lack of data
    if (!snapshot.hasData) {
      return const Center(
        child: Text("Something when wrong - no data available"),
      );
    }

    return ListView.builder(
      itemCount: snapshot.data!.docs.length,
      itemBuilder: (context, index) => Container(
        margin: EdgeInsets.symmetric(
            horizontal: width > webScreenSize ? width * 0.3 : 0,
            vertical: width > webScreenSize ? 15 : 0),
        child: PostCard(
          snap: snapshot.data!.docs[index],
        ),
      ),
    );

Upvotes: 0

Related Questions