0

I am trying to retrieve a UID from Firebase and return it to a variable. However I am unable to get it working. Could someone please help on this. Thanks

getUID() async {
    final userId = await widget.auth.getCurrentUser();
    print('checking user id ${userId.uid}');
    return userId.uid;
}

@override
void initState() {
  super.initState();
  void foo() async {
    final userID = await getUID();
    print("UID is ${userID}");
  }
  foo();
  print("UID is ${userID}");
}
5
  • Define "unable to get it working", what errors are you getting? What line do these errors occur? What happens when you inspect using a debugger? Commented Sep 12, 2020 at 15:25
  • Apologies. When I try to print it as showin in the last line it says as userID is undefined. I need to get the value outside from foo function to pass on to other pages. Commented Sep 12, 2020 at 15:26
  • please add code of getcurrentuser Commented Sep 12, 2020 at 15:30
  • This is the code of getCurrentUser from auth.dart file Future<FirebaseUser> getCurrentUser() async { FirebaseUser user = await _firebaseAuth.currentUser(); return user; } Commented Sep 12, 2020 at 15:36
  • Asnwered , comment if something goes wrong Commented Sep 12, 2020 at 15:57

3 Answers 3

1

The issue you are running into is scope. You declare userId within the inner function foo, so that variable doesn't exist outside of foo. If you want to access it within the scope of initState, you need to declare it within that scope:

@override
void initState() {
  super.initState();
  String userId;

  void foo() async {
    userID = await getUID();
    print("UID is ${userID}");
  }

  foo();
  print("UID is ${userID}");
}
Sign up to request clarification or add additional context in comments.

4 Comments

After invoking the foo() function and trying to print it shows as "UID is null". Although inside the foo function the value gets print, but not able to get it outside.
@RaghuCK The issue is that foo is marked as async, but when you call foo, you don't await it. This causes initState to just breeze past it without waiting for the results. Normally I'd say just make initState asynchronous, but in this case, Flutter will not allow you to do that because all async functions return Future and initState must return void.
Thanks. Could you please help how to get the userID value into a variable so I can pass on to other pages. Or else please let me know how to get the uid of a user from firebase.
@RaghuCK Put it in a separate function that you can mark as async. Then you can await on foo and set the result to a variable all you want. Although in this situation, foo is the separate function and the userID is really coming from getUID.
0

Declare this Outside every Class

String getCurrentUser(){
return FirebaseAuth.instance.currentUser;
}

and the class in which you are using it should be

class RootPage extends StatefulWidget {

@override
State<StatefulWidget> createState() => new _RootPageState();
}
class _RootPageState extends State<RootPage> {
String _userId;

@override
void initState() {
super.initState();
_userId=getCurrentUser();
}

@override
Widget build(BuildContext context) {
return Text(_userId==null?"NO User Logged In":_userId);
}

Also note with latest version of firebaseauth this function no longer returns a future;

Comments

0
/// Create a String variable to hold the value of the user UID
String userId; 

void getCurrentUser() {
try {
  User loggedInUser = _auth.currentUser;
  if (loggedInUser != null) {
    userId = loggedInUser.uid;
  }
} catch (e) {
  print(e.toString());
}

}

@override
void initState() {
  super.initState();
  getCurrentUser(); // The code to get the currrent user is run in the initState and user UID stored in the 'userId' variable

}

You now have access to the currently logged in user UID anywhere in your current Class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.