hromer
hromer

Reputation: 181

Getting the result of a method Future<T> flutter

I have a method

Future<FirebaseUser> getCurrentUser() async {
    FirebaseUser user = await _firebaseAuth.currentUser();
    return user;
}

I want to be able to call it in some other module by doing :

if (class.getCurrentUser() != null) {
    // Do something
}

I cannot seem to figure out how to get the actual value, and not the Future object storing the value. In C++ for instance, I can just do future.get(), which will block and return the value to me. Is there an equivalent in flutter? I am new to the language and have searched for hours and cannot seem to find a solution to this exact problem.

Upvotes: 1

Views: 1533

Answers (2)

TruongSinh
TruongSinh

Reputation: 4856

From your code, it seems you actually need 1 await only:

Future<FirebaseUser> getCurrentUser() async {
    return _firebaseAuth.currentUser();
}
if (await class.getCurrentUser() != null) {
    // Do something
}

Upvotes: 1

dshukertjr
dshukertjr

Reputation: 18603

You have to await for the result in your method call as well like this:

FirebaseUser currentUser = await class.getCurrentUser();
if (currentUser != null) {
    // Do something
}

Upvotes: 1

Related Questions