How to get the current user id from Firebase in Flutter

firebase get current user email
flutter keep user logged in
flutter firebase
flutter firebase auth
flutter firebase check if user is logged in
flutter firestore get document
flutter firebase delete user
flutter check if user is logged in

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?

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:

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

How to get the current user id from Firebase in Flutter, 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: void inputData() async  SOLUTION 1 : 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: void inputData () async {. final FirebaseUser user = await auth.currentUser (); final uid = user.uid.

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
});

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  As you can see above, the new user’s uid is used as the document id of that specific user’s document in the ‘users’ collection. So, to recap. We were able to: Allow user authentication from the Firebase Console; Integrate Firebase Authentication in the app; Change the Navigation flow of the App to include the authentication process

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'),
            ),

Manage Users in Firebase, The recommended way to get the current user is by setting a listener on the Auth System.Uri photo_url = user.PhotoUrl; // The user's Id, unique to the Firebase  This async _getCurrentUser method get the user from the FirebaseAuth and repaints the account status variable on the screen; Once you signIn with email and password then the user is easily obtained from the async method. Hope this serves the question Note that this works even when the internet is off.

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;
}

Firebase Startup Logic and Custom User Profiles - Flutter , The recommended way to get the current user is by calling the The user's ID, unique to the Firebase project. Do NOT use Id of the provider (ex: google.com) I'm new to flutter and firebase so bear with me. I'm using email sign up with firestore and flutter on my app, on registration some additional fields are saved to firestore. I want to retrieve those fields to display on the user profile. The key identifier for the fields saved to the users collection is the auto generated user id upon sign up.

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

Firebase Authentication with Flutter, In part 2 of the Firebase and Flutter series we will be adding custom start up logic, service would provide you with data based on a user's id or authentication token. Firebase Auth provides you with a current user to check. Firebase.Auth.FirebaseUser user = auth.CurrentUser; // Get auth credentials from the user for re-authentication. The example below shows // email and password credentials but there are multiple

Firebase Authentication currentUser() returns null · Issue #17805 , I'm going to interact with firebase to authenticate the user and do some more For android go to the Firebase console and click on add app button and password: password); return user.uid; } Future<String> signUp(String currentUser(); return user; } Future<void> signOut() async { return _firebaseAuth. Get the currently signed-in user The recommended way to get the current user is by setting an observer on the Auth object: firebase.auth ().onAuthStateChanged (function (user) { if (user) { // User

Firebase User Authentication in Flutter, This is about Flutter Firebase Authentication plugin. I am trying to I don't know, how can i use your code to get the current UserID and i have  startup logic and user profiles in Flutter. In part 2 of the Firebase and Flutter series we will be adding custom start up logic, user profiles and making sure it’s available everywhere from start.

How is FirebaseAuth current user persisted : Firebase, Run flutter packages get once more to install the required packages. method returns the logged-in user's information, like their unique id (uid). If the currentUser object isn't found, the user is directed to the Login page. This is about Flutter Firebase Authentication plugin. I am trying to send a verification email after creating a new user, but sendEmailVerification() internally uses currentUser().

Comments
  • Do you understand how you're supposed to use Futures in dart?
  • OK, that's something you're going to have to learn if you want to write apps with Flutter. It's how you do asynchronous programming. If you don't understand that, you're not going to get very far.
  • But at least tell me the solution to this problem...
  • 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.
  • You may want to read here as well to learn about asynchronous programming in Dart. Without this knowledge, you won't get very far: dartlang.org/tutorials/language/futures
  • It doesnt return string..