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?
firebase dart firebase-authentication

|
show 2 more comments
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

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
|
show 2 more comments
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

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

firebase dart firebase-authentication

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
|
show 2 more comments
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
|
show 2 more comments
3 Answers
3
active
oldest
votes
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
}
add a comment |
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
});
add a comment |
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;
}
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
}
add a comment |
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
}
add a comment |
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
}
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
}
answered Jan 2 at 4:10
dshukertjrdshukertjr
1,9671930
1,9671930
add a comment |
add a comment |
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
});
add a comment |
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
});
add a comment |
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
});
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
});
answered Jan 2 at 9:49


Krishnakumar CNKrishnakumar CN
426315
426315
add a comment |
add a comment |
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;
}
add a comment |
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;
}
add a comment |
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;
}
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;
}
edited Jan 2 at 7:21
answered Jan 2 at 6:51


satishsatish
123115
123115
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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