1

I'm new to Firebase and I managed to finish authentication in my flutter app using Firebase as cloud database and it works perfectly fine. But how do I get the current user data? My situation/problem goes something like this.

  • User A logged in and get username A(their name)
  • User B logged in and still get username A(User A name)

the situation is like I logged in Facebook but I entered someone else's profile because I read the same collection and document. This is my code of how it goes

CollectionReference users = FirebaseFirestore.instance.collection('Harris');

return FutureBuilder<DocumentSnapshot>(
        future: users.doc('username').get(),
        builder: (BuildContext context,
            AsyncSnapshot<DocumentSnapshot> snapshot) {
          if (snapshot.hasError) {
            return Center(child: Text("Something went wrong"));
          }

          if (snapshot.hasData && !snapshot.data!.exists) {
            return Center(child: Text("Document does not exist"));
          }

          if (snapshot.connectionState == ConnectionState.done) {
            Map<String, dynamic> data =
                snapshot.data!.data() as Map<String, dynamic>;
            return Container(
                height: double.infinity,
                width: double.infinity,
                decoration: BoxDecoration(
                  color: brightGray,
                ),
                child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(
                        'Welcome,',
                        style: ktextFont3,
                      ),
                      Text(
                        '${data['username']}',
                        style: ktextFont4,
                      ),]));}
return Center(
              child: CircularProgressIndicator(
            color: mintGreen,
          ));
        },
      );

this is my Firebase Console

Firebase Console

I have tried to replace the collection and document with username variable but it shows null when I hot restart the app. Any advice/assist is appreciated. Thank you

1 Answer 1

1

If userA signed in you can get name,email,firebase uid and image from this line of code

Firebase id : FirebaseAuth.instance.currentUser!.uid

Firebase name : FirebaseAuth.instance.currentUser!.displayName

Firebase email : FirebaseAuth.instance.currentUser!.email

Firebase photo_url : FirebaseAuth.instance.currentUser!.photoURL

Sign up to request clarification or add additional context in comments.

5 Comments

What if I just want to get a data string or integer?
then you have to save your values in your own collection and fetch it from there.
lets say user A type in their name in name form in the app and they proceed to the next page. Do I need to pass the name variable to the next page? that way the app can detect what data needs to be collect from Firestore?
If you want to collect the data so you have to use get() with query .where-> firbaseUid
Thank you for the solution you provide. I can finally get the data by using Firebase UID. It solves my problem!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.