Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 822
Sabih Khan Avatar asked Jan 02 '19 03:01

Sabih Khan


People also ask

How do I find my Firebase user ID?

You create a new user in your Firebase project by calling the createUserWithEmailAndPassword method or by signing in a user for the first time using a federated identity provider, such as Google Sign-In or Facebook Login.

How do I get Firebase Flutter ID?

You will need to 'await' the Future to get a hold of the FirebaseUser object it will contain when the async work is done. That FirebaseUser object will have a property for the UID of the user. You should probably consult the Firebase Auth API docs for Flutter for more details.

What does Firebase auth () CurrentUser return?

If a user isn't signed in, CurrentUser returns null. Note: CurrentUser might also return null because the auth object has not finished initializing.


7 Answers

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
}
like image 196
dshukertjr Avatar answered Oct 11 '22 17:10

dshukertjr


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;
}
like image 26
satish Avatar answered Oct 11 '22 17:10

satish


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'),
            ),
like image 36
Ron Avatar answered Oct 11 '22 17:10

Ron


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
});
like image 21
krishnakumarcn Avatar answered Oct 11 '22 16:10

krishnakumarcn


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

like image 22
XHFKA Avatar answered Oct 11 '22 15:10

XHFKA


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
}
like image 30
CopsOnRoad Avatar answered Oct 11 '22 15:10

CopsOnRoad


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();
like image 38
Junaid Shaikh Avatar answered Oct 11 '22 16:10

Junaid Shaikh