Home > Mobile >  flutter: how can I retrieve current user data from firebase firestore
flutter: how can I retrieve current user data from firebase firestore

Time:10-25

am trying to retrieve current user data but it showing all the users data from firestore in app here is my code

 Widget build(BuildContext context){
 CollectionReference users = FirebaseFirestore.instance.collection('users');
  return FutureBuilder<DocumentSnapshot>(

  future: users.doc(documentId).get(),
  builder: ((context, snapshot){ 
    if(snapshot.connectionState == ConnectionState.done){
      Map<String, dynamic> data = 
      snapshot.data!.data() as Map<String, dynamic>; 
      return Container(
        child: Column(
          children: [
            Text('Email : '"${data['Email name'].toString()}",
            style: const TextStyle(fontWeight: FontWeight.bold,wordSpacing: 10),),
            const SizedBox(
              width: 10,
            ),
            Text('Contact : '"${data['age'].toString()}",
            style: const TextStyle(fontWeight: FontWeight.bold,wordSpacing: 10),),
            const SizedBox(
              width: 10,
            ),
            Text('Name : '"${data['firstname'].toString()}",
            style: const TextStyle(fontWeight: FontWeight.bold,wordSpacing: 10),),
            const SizedBox(
              width: 10,
            ),
            Text('Last Name : '"${data['lastname'].toString()}",
            style: const TextStyle(fontWeight: FontWeight.bold,wordSpacing: 10),),
            const SizedBox(
              width: 10,
            ),

          ],
        ),
      );
    }
    return const Text('Loading');
  }),

here is documentId code

  List<String> docIDs = [];

  Future getDocId() async{
  await FirebaseFirestore.instance.collection('users').get().then(
  (snapShot) => snapShot.docs.forEach(
    (documant){
    print(documant.reference);
    docIDs.add(documant.reference.id);
    }));
    }

     body: Center(
     child: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        // ignore: prefer_const_constructors
        Text('SignUp as ${user!.email!}',style: TextStyle(
          fontSize: 20,
        ),),
        Expanded(child: FutureBuilder(
          future: getDocId(),
          builder: (context, snapshot){
            return ListView.builder(
              itemCount: docIDs.length,
              itemBuilder: (context, index){
                return ListTile(
                  title: GetuserData(documentId: docIDs[index]),
                );
              },
            );
          },
        ))

when ever user post the form it submitted successfully and generate a post like summited form history but it showing to others users to but i wont it for only current user how to do that please any one can help me

CodePudding user response:

You can use firestore query to return the current user using where()

on getDocId():

await FirebaseFirestore.instance.collection('users').get()

change to

await FirebaseFirestore.instance.collection('users')
                       .where('Email name', isEqualTo: '[email protected]').get()

you can replace the email with your or consider using parameter to pass user email dynamically.

  • Related