Home > Software engineering >  How would I go about getting another user's UID in flutter with Firestore?
How would I go about getting another user's UID in flutter with Firestore?

Time:07-11

I've been trying to implement a send friend request feature in my app made with flutter.

However, I ran into an issue as I don't know how I am supposed retrieve another user's UID when they press the List Tile. I am using a document to store each individual user inside of a collection called "users".

My goal is to be able to create a document inside of another collection called "friendships" that has one document that represents two users in a friendship by having the senders UID and the receiver's UID in the fields.

Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: Text('Home'),
    elevation: 0,
  ),
  body: Container(
    child: StreamBuilder(
      stream: FirebaseFirestore.instance.collection('users').snapshots(),
      builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.hasData) {
          return ListView.builder(

            itemCount: snapshot.data?.docs.length,
              itemBuilder: (context, index) =>
                  Padding(
                    padding: const EdgeInsets.all(5.0),
                    child: ListTile(
                      shape: RoundedRectangleBorder(
                        side: BorderSide(color: Colors.grey),
                          borderRadius: BorderRadius.circular(5),
                      ),
                      leading: CircleAvatar(backgroundColor: Colors.lightBlue,
                ),
                      title: Text(snapshot.data?.docs[index]['firstname']   " "   snapshot.data?.docs[index]['lastname']),
                      subtitle: Text(snapshot.data?.docs[index]['email']),
                      trailing: Icon(Icons.keyboard_arrow_right, color: Colors.grey),
                      onTap: () {
                        

                      },

                    )

                  ),

          );
        } else {
          return Container();
        }
      },
    ),
  ),

);

}

CodePudding user response:

You should add UID inside each person's doc.

For example like this. Then you can make function in onTap{}.

final FirebaseFirestore _firestore = FirebaseFirestore.instance;

await _firestore.collection('users').doc(snapshot.data?.docs[index]['uid']).get().then((value){
  friendid = value['uid'],
  friendnick = value['nick'], //You can access the uid of the other user with this nuance.
});
  • Related