Sabih Khan
Sabih Khan

Reputation: 537

How to get the current user id from Firebase in Flutter

I am trying this -

final Future<FirebaseUser> user = auth.currentUser();

but the problem is that instead of making a document by the "userid" it is making a document by the name of -

Instance of 'Future<FirebaseUser>'

This is literally my documents name right now, but I want to make it the userid specifically.

What should I do?

Upvotes: 36

Views: 133878

Answers (10)

CopsOnRoad
CopsOnRoad

Reputation: 268484

Update (Null safe)

To get uid, email, and other information about the user, check if the user is signed in and then retrieve these values from the User class.

User? user = FirebaseAuth.instance.currentUser;

// Check if the user is signed in
if (user != null) {
  String uid = user.uid; // <-- User ID
  String? email = user.email; // <-- Their email
}

Upvotes: 7

dshukertjr
dshukertjr

Reputation: 18750

Update (2020.09.09)

After firebase_auth version 0.18.0

Few breaking updates were made in firebase_auth 0.18.0. FirebaseUser is now called User, currentUser is a getter, and currentUser is synchronous.

This makes the code for getting uid like this:

final FirebaseAuth auth = FirebaseAuth.instance;

void inputData() {
  final User user = auth.currentUser;
  final uid = user.uid;
  // here you write the codes to input the data into firestore
}

Before firebase_auth version 0.18.0

uid is a property of FirebaseUser object. Since auth.currentUser() return a future, you have to await in order to get the user object like this:

final FirebaseAuth auth = FirebaseAuth.instance;

Future<void> inputData() async {
  final FirebaseUser user = await auth.currentUser();
  final uid = user.uid;
  // here you write the codes to input the data into firestore
}

Upvotes: 99

Camilo Lizarazo Olaya
Camilo Lizarazo Olaya

Reputation: 5831

This is a very comprehensive resource to solve all issues related to registration and login in Flutter with updated Firebase code. https://kickertech.com/login-and-register-easily-with-flutter-using-firebase/

Upvotes: 0

Carl Angelo Angcana
Carl Angelo Angcana

Reputation: 113

You may add this into the global state before your flutter widget.

final FirebaseAuth auth = FirebaseAuth.instance;
final User user = auth.currentUser;
final myUid = user.uid;

it will make myUid available for this situation.

Upvotes: 1

Junaid Shaikh
Junaid Shaikh

Reputation: 31

you can also try this.

     currentUser() {
         final User user = _firebaseAuth.currentUser;
         final uid = user.uid.toString();
         return uid;
      }

now just call currentUser() in your code and it will return uid of the currently logged-in user.

    uid=FirebaseAuthService().currentUser();

Upvotes: 1

OmkarKhilari
OmkarKhilari

Reputation: 62

Doing it normaly will always return

Instance of 'Future<FirebaseUser>'

Try using .then as follows

First Create function in your authservice class

Future<String> getCurrentUID() async{
    final FirebaseUser user = await _auth.currentUser();
    final String uid = user.uid;
    return uid;
  }

Then call this function where you wnat this ID as Following

Widget build(BuildContext context) {
    void getid(String foo) {
      userID = foo;
    }

    AuthService().getCurrentUID().then((value) => getid(value));

    return Scaffold(
      ...
    );
}

This is most convenient method to get the ID You can also get email and mobile no. like this.

Upvotes: 1

Ron
Ron

Reputation: 271

final FirebaseAuth _auth = FirebaseAuth.instance;
getCurrentUser() async {
    final FirebaseUser user = await _auth.currentUser();
    final uid = user.uid;
    // Similarly we can get email as well
    //final uemail = user.email;
    print(uid);
    //print(uemail);
  }

Call the function getCurrentUser to get the result. For example, I used a button:

RaisedButton(
              onPressed: getCurrentUser,
              child: Text('Details'),
            ),

Upvotes: 5

krishnakumarcn
krishnakumarcn

Reputation: 4199

You need to wait for the asynchronous operation to complete.

final FirebaseUser user = await auth.currentUser();
final userid = user.uid;

or you can use the then style syntax:

final FirebaseUser user = auth.currentUser().then((FirebaseUser user) {
  final userid = user.uid;
  // rest of the code|  do stuff
});

Upvotes: 5

XHFKA
XHFKA

Reputation: 5

This is another way of solving it:

Future<String> inputData() async {
    final FirebaseUser user = await FirebaseAuth.instance.currentUser();
    final String uid = user.uid.toString();
  return uid;
  }

it returns the uid as a String

Upvotes: 2

satish
satish

Reputation: 1374

If you are using sign in with Google than you will get this info of user.

final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
final GoogleSignIn _googleSignIn = new GoogleSignIn();
void initState(){
super.initState();
 firebaseAuth.onAuthStateChanged
        .firstWhere((user) => user != null)
        .then((user) {
      String user_Name = user.displayName;
      String image_Url = user.photoUrl;
      String email_Id = user.email;
      String user_Uuid = user.uid; // etc
      }
       // Give the navigation animations, etc, some time to finish
    new Future.delayed(new Duration(seconds: 2))
        .then((_) => signInWithGoogle());
        }

     Future<FirebaseUser> signInWithGoogle() async {
  // Attempt to get the currently authenticated user
  GoogleSignInAccount currentUser = _googleSignIn.currentUser;
  if (currentUser == null) {
    // Attempt to sign in without user interaction
    currentUser = await _googleSignIn.signInSilently();
  }
  if (currentUser == null) {
    // Force the user to interactively sign in
    currentUser = await _googleSignIn.signIn();
  }

  final GoogleSignInAuthentication googleAuth =
      await currentUser.authentication;

  // Authenticate with firebase
  final FirebaseUser user = await firebaseAuth.signInWithGoogle(
    idToken: googleAuth.idToken,
    accessToken: googleAuth.accessToken,
  );

  assert(user != null);
  assert(!user.isAnonymous);

  return user;
}

Upvotes: 5

Related Questions