sabri bargougui
sabri bargougui

Reputation: 35

How To get all Users from FireStore Using flutter?

I want to display all the users of my firestore database as a list. My FireStore Users Collection

I tried this but some errors appear.

  StreamBuilder(
          stream:chatservice.retrieveUsers(),
          builder: (ctx, usersnapshot) {
            if (usersnapshot.connectionState == ConnectionState.waiting) {
              return Container(
                  child: Center(child: CircularProgressIndicator()));
            } else {
              var document = usersnapshot.data;
              return  ListView.builder(
          itemCount: document.length,
          shrinkWrap: true,
          padding: EdgeInsets.only(top: 16),
          physics: NeverScrollableScrollPhysics(),
          itemBuilder: (context, index) {
            return ConversationList(
              name: document[index]['name'],       
            );
          },
        );

            }
          },
        )

chatservice.retrieveUsers():

  Stream<QuerySnapshot> retrieveUsers() {
Stream<QuerySnapshot> queryUsers = _usercollection.snapshots();

return queryUsers;
  }

Upvotes: 0

Views: 1286

Answers (1)

sabri bargougui
sabri bargougui

Reputation: 35

i got this solution , this is work great ^^

 StreamBuilder<QuerySnapshot>(
        stream: FirebaseFirestore.instance.collection('Users').snapshots(),
        builder: (ctx, AsyncSnapshot<QuerySnapshot> usersnapshot) {
          if (usersnapshot.connectionState == ConnectionState.waiting) {
            return Container(
                child: Center(child: CircularProgressIndicator()));
          } else {
            return Expanded(
              child: ListView.builder(
                itemCount: usersnapshot.data.docs.length,
                itemBuilder: (context, index) {
                  DocumentSnapshot document = usersnapshot.data.docs[index];
                  if (document.id == auth.currentUser.uid) {
                    return Container(height: 0);
                  }
                  return ConversationList(
                    name: document.data()["name"] +
                        " " +
                        document.data()["lastname"],
                    messageText: document.data()["lastname"],
                    imageUrl:
                        "https://cdn3.vectorstock.com/i/1000x1000/30/97/flat-business-man-user-profile-avatar-icon-vector-4333097.jpg",
                    isMessageRead: false,
                  );
                },
              ),
            );
          }
        },
      )

Upvotes: 1

Related Questions