简体   繁体   中英

Use StreamBuilder with orderBy and where

I have collection for Users and groups, I want to display list of last update groups. But, the user must be stay in member list of groupCollection.

There is a screenshot of group collection在此处输入图像描述

So, I used StreamBuilder

StreamBuilder(
        stream: FirebaseFirestore.instance
            .collection("groups")
            .orderBy('LastUpdate', descending: true)
            .where(
              "members",
              isEqualTo: FirebaseAuth.instance.currentUser!.uid,
            )
            .snapshots(),
        builder: (context, AsyncSnapshot snapshot) {
          if (snapshot.hasData) {
            if (snapshot.data['members'] != null) {
              if (snapshot.data['members'].length != 0) {
                return Padding(
                  padding: const EdgeInsets.only(top: 5),
                  child: ListView.builder(
                    itemCount: snapshot.data['members'].length,
                    itemBuilder: (context, index) {
                      return Text(
                        snapshot.data["groupName"],
                      );
                    },
                  ),
                );
              } else {
                return noGroupWidget();
              }
            } else {
              return noGroupWidget();
            }
          } else {
            return Center(
              child: CircularProgressIndicator(color: Colors.white),
            );
          }
        });

This showing always white color CircularProgressIndicator

You're ignoring errors. When dealing with any AsyncSnapshot , always check for errors. For example with:

builder: (context, AsyncSnapshot snapshot) {
  if (snapshot.hasError) {
    return Text('${snapshot.error}');
  }
  if (snapshot.hasData) {
    ...

This will tell you the root cause of the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM