Home > Mobile >  flutter firebase fetching data
flutter firebase fetching data

Time:12-12

i'm trying to search for all users from firebase and i have done that by getting all users from firestore but the problem is that i dont want myself to appear between the users in search bar...in other way..i want to get all users except myself so how to do it in this code ?

Widget buildResults(BuildContext context) {
  return StreamBuilder<QuerySnapshot>(
    stream: FirebaseFirestore.instance.collection('users').snapshots(),
    builder: (context, snapshots){
      return(snapshots.connectionState == ConnectionState.waiting)
          ? Center(
        child: CircularProgressIndicator(),
      )
          : ListView.builder(
          itemCount: SocialCubit.get(context).users.length,
          itemBuilder: (context, index) {
            var data = snapshots.data!.docs[index].data()
            as Map<String, dynamic>;

            if (query.isEmpty) {
              return ListTile(
                title: Text(
                  data['name'],
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                      color: Colors.black54,
                      fontSize: 16,
                      fontWeight: FontWeight.bold),
                ),
                subtitle: Text(
                  data['bio'],
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                      color: Colors.black54,
                      fontSize: 16,
                      fontWeight: FontWeight.bold),
                ),
                leading: CircleAvatar(
                  backgroundImage: NetworkImage(data['image']),
                ),
              );
            }
            if (data['name']
                .toString()
                .toLowerCase()
                .startsWith(query.toLowerCase())) {
              return ListTile(
                onTap: ()
                {
                  Navigator.push(context, MaterialPageRoute(builder: (context)=> ChatDetailsScreen(SocialCubit.get(context).users[index])));
                },
                title: Text(
                  data['name'],
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                      color: Colors.black54,
                      fontSize: 16,
                      fontWeight: FontWeight.bold),
                ),
                subtitle: Text(
                  data['bio'],
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                      color: Colors.black54,
                      fontSize: 16,
                      fontWeight: FontWeight.bold),
                ),
                leading: CircleAvatar(
                  backgroundImage: NetworkImage(data['image']),
                ),
              );
            }
            return Container();
          });
    },
  );
}

is there a way to implement it on the above code ? here is my data in firestore

CodePudding user response:

you can exclude your uid from the Query, with the where() filtering method like this:

return StreamBuilder<QuerySnapshot>(
  stream: FirebaseFirestore.instance.collection('users').where("uId", isNotEqualTo: FirebaseAuth.instance.currentUser!.uid).snapshots(),
   // ...

this will return all documents of your users collection which have an uid property different than your uid for from Firebase auth.

  • Related