In my AuthProvider class where I handle my sign in, sign, out authentications, I created 2 functions that returns a Future String like so
Future<String> currentUser() async {
FirebaseUser user = await _auth.currentUser();
return user.uid;
}
Future<String> getCurrentUserEmail() async {
FirebaseUser user = await _auth.currentUser();
final String email = user.email.toString();
// print(email);
return email;
}
In my menu screen, I want to display my current signed in user email in a text field and I am calling it as below.
UserAccountsDrawerHeader(
accountName: Text('Brad Pitt'),
accountEmail: Text(
'${AuthProvider.of(context).auth.getCurrentUserEmail()}'),
I have tried using both the currenUser() and getCurrentUserEmail() to try to display the loggedIn user's email but I keep getting a "Instance of Future" displayed.
Is there something I'm overlooking here? I've tried every possible hack I can think of.
Thanks.
Since your getCurrentUserEmail
returns a Future
, you'll need to use a FutureBuilder
to use it in your build method.
accountEmail: FutureBuilder<String>(
future: AuthProvider.of(context).auth.getCurrentUserEmail(),
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
if (snapshot.hasData) {
return Text(snapshot.data)
}
else {
return Text("Loading user data...")
}
}
)
The best thing to do is to upgrade to firebase_auth:0.18.0
, after upgrade you can get the currentUser
synchronously!
dependencies:
flutter:
sdk: flutter
firebase_core : ^0.5.0
firebase_auth : ^0.18.0
initialize Firebase:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
Then in UsersAccountDrawerHeader
:
UserAccountsDrawerHeader(
accountName: Text('Brad Pitt'),
accountEmail: Text('${auth.instance.currentUser.email}'),
Also check:
Undefined class 'FirebaseUser'
No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp() in Flutter and Firebase
You need to add ~await~ in front of the function as it's a function that returns a ~Future~
await AuthProvider.of(context).auth.getCurrentUserEmail()
Retrieving user email, null safety supported.
var currentUser = FirebaseAuth.instance.currentUser;
Text('admin email: ${FirebaseAuth.instance.currentUser!.email}'),
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.