How to get the current user id from Firebase in Flutter












1















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?










share|improve this question




















  • 1





    Do you understand how you're supposed to use Futures in dart?

    – Doug Stevenson
    Jan 2 at 3:16











  • Being totally honest with you..No..Actually I am new to it .. previously I was doing android java..so yeah .. NO

    – Sabih Khan
    Jan 2 at 3:32






  • 1





    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.

    – Doug Stevenson
    Jan 2 at 3:37






  • 1





    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.

    – Doug Stevenson
    Jan 2 at 4:08






  • 1





    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

    – Doug Stevenson
    Jan 2 at 4:10
















1















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?










share|improve this question




















  • 1





    Do you understand how you're supposed to use Futures in dart?

    – Doug Stevenson
    Jan 2 at 3:16











  • Being totally honest with you..No..Actually I am new to it .. previously I was doing android java..so yeah .. NO

    – Sabih Khan
    Jan 2 at 3:32






  • 1





    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.

    – Doug Stevenson
    Jan 2 at 3:37






  • 1





    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.

    – Doug Stevenson
    Jan 2 at 4:08






  • 1





    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

    – Doug Stevenson
    Jan 2 at 4:10














1












1








1








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?










share|improve this question
















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?







firebase dart firebase-authentication flutter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 3:23









Frank van Puffelen

241k29386413




241k29386413










asked Jan 2 at 3:13









Sabih KhanSabih Khan

155




155








  • 1





    Do you understand how you're supposed to use Futures in dart?

    – Doug Stevenson
    Jan 2 at 3:16











  • Being totally honest with you..No..Actually I am new to it .. previously I was doing android java..so yeah .. NO

    – Sabih Khan
    Jan 2 at 3:32






  • 1





    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.

    – Doug Stevenson
    Jan 2 at 3:37






  • 1





    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.

    – Doug Stevenson
    Jan 2 at 4:08






  • 1





    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

    – Doug Stevenson
    Jan 2 at 4:10














  • 1





    Do you understand how you're supposed to use Futures in dart?

    – Doug Stevenson
    Jan 2 at 3:16











  • Being totally honest with you..No..Actually I am new to it .. previously I was doing android java..so yeah .. NO

    – Sabih Khan
    Jan 2 at 3:32






  • 1





    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.

    – Doug Stevenson
    Jan 2 at 3:37






  • 1





    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.

    – Doug Stevenson
    Jan 2 at 4:08






  • 1





    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

    – Doug Stevenson
    Jan 2 at 4:10








1




1





Do you understand how you're supposed to use Futures in dart?

– Doug Stevenson
Jan 2 at 3:16





Do you understand how you're supposed to use Futures in dart?

– Doug Stevenson
Jan 2 at 3:16













Being totally honest with you..No..Actually I am new to it .. previously I was doing android java..so yeah .. NO

– Sabih Khan
Jan 2 at 3:32





Being totally honest with you..No..Actually I am new to it .. previously I was doing android java..so yeah .. NO

– Sabih Khan
Jan 2 at 3:32




1




1





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.

– Doug Stevenson
Jan 2 at 3:37





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.

– Doug Stevenson
Jan 2 at 3:37




1




1





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.

– Doug Stevenson
Jan 2 at 4:08





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.

– Doug Stevenson
Jan 2 at 4:08




1




1





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

– Doug Stevenson
Jan 2 at 4:10





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

– Doug Stevenson
Jan 2 at 4:10












3 Answers
3






active

oldest

votes


















2














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
}





share|improve this answer































    1














    You need to wait for the asynchronous operation to complete.



    final FirebaseUser user = awit 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
    });





    share|improve this answer































      0














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





      share|improve this answer

























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000825%2fhow-to-get-the-current-user-id-from-firebase-in-flutter%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        2














        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
        }





        share|improve this answer




























          2














          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
          }





          share|improve this answer


























            2












            2








            2







            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
            }





            share|improve this answer













            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
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 2 at 4:10









            dshukertjrdshukertjr

            1,9671930




            1,9671930

























                1














                You need to wait for the asynchronous operation to complete.



                final FirebaseUser user = awit 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
                });





                share|improve this answer




























                  1














                  You need to wait for the asynchronous operation to complete.



                  final FirebaseUser user = awit 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
                  });





                  share|improve this answer


























                    1












                    1








                    1







                    You need to wait for the asynchronous operation to complete.



                    final FirebaseUser user = awit 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
                    });





                    share|improve this answer













                    You need to wait for the asynchronous operation to complete.



                    final FirebaseUser user = awit 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
                    });






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 2 at 9:49









                    Krishnakumar CNKrishnakumar CN

                    426315




                    426315























                        0














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





                        share|improve this answer






























                          0














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





                          share|improve this answer




























                            0












                            0








                            0







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





                            share|improve this answer















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






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jan 2 at 7:21

























                            answered Jan 2 at 6:51









                            satishsatish

                            123115




                            123115






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000825%2fhow-to-get-the-current-user-id-from-firebase-in-flutter%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                MongoDB - Not Authorized To Execute Command

                                How to fix TextFormField cause rebuild widget in Flutter

                                in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith