Reputation: 3400
Can we return or get the value from a Future object in dart. By reading the docs i could understand that we can pass a function in then method of Future class, but still i am clueless how can we return the value.
Consider following as pseudo code
Int getTotalPay(){
Future<int> pension = getPensionFundAPI(); // a long running api call. don't wait here and call to next api
int salary = getTotalSalary();
//wait here till we get the int value received in Future when the above api call completes.
int result = salary + pension;
print(result);
return result; }
i know i can make call to Future.then and pass a callback method in there. but my purpose is to return the result outside of then. same as we do in java and other oops language.
is it possible in dart?
Upvotes: 0
Views: 4587
Reputation: 417
What I understand your question is: you don't want your code to stop and wait for the future to complete, instead in the meantime, while it completes, the code should continue excecuting/calculating other stuff. And if this other stuff is done before your future is, only then do you want it to wait.
This may be usefull when the code inside the future will have to wait itself (examples include a Network call or reading from disc) and you want to use that waiting time to calculate the rest.
For that I would recommend Future.wait()
.
Simply wrap the 'other code' inside a method returning a future itself and pass them both to Future.wait()
. This takes a list of futures, excecute all simultaneously and return all of their results once all are complete. For more information see: Dart Docs: Future class - wait method.
Example code, inspired by the example on the documentation:
int getTotalPay() async {
List<int> values = await Future.wait([getPensionFundAPI(), getTotalSalary()]);
int totalPay = values[0] + values[1];
print(totalPay );
return totalPay;
}
Future<int> getPensionFundAPI() async {
// TODO: implement proper pension api
int pension = 2;
await Future.delayed(const Duration(seconds: 5));
return pension;
}
Future<String> getTotalSalary() async {
// TODO: implement proper salary calculating
int salary = 3;
await Future.delayed(const Duration(seconds: 1));
return salary;
}
Upvotes: 2
Reputation: 2254
You can use await
, but you method needs async
.
I think that your method will be like this:
void main() async {
int pension = await getPensionFundAPI(); // a long running api call. don't wait here and call to next api
int salary = getTotalSalary();
//wait here till we get the int value received in Future when the above api call completes.
int result = salary + pension;
print(result);
}
so await will block the method until the value is returned.
Upvotes: 0
Reputation: 12014
It's not possible. Just like bringing your 80 year old self to here and now is not possible :)
However, you can write code that looks like it's possible, but actually is running different parts of a single function in different times. For that, you need to learn about the async
and await
keywords.
Upvotes: 1