Reputation: 6148
eg:
Future<int> _test1() async {
return Future.delayed(Duration(seconds: 1)).then((value) {
return 1;
});
}
void _onTapButton() async {
await _test1().then((value) {
print('a');
if (value == 1) {
print('return');
return;
}
});
print('b');
}
When I call _onTapButton
, console print a
return
b
not a
return
.
That is to say the return
in _test1().then
didn't return the function _onTapButton
.
Is there any way I can return function _onTapButton
in _test1().then
?
Upvotes: 1
Views: 1094
Reputation: 9734
In your code then
part is a callback that will be executed upon completion of your future, and return
is meant in the scope of this callback, not your _onTapButton
function.
If you want to wait for the future, and if it results 1, print return
and return from _onTapButton
, you can try this:
void _onTapButton() async {
final value = await _test1();
print('a');
if (value == 1) {
print('return');
return;
}
print('b');
}
Upvotes: 2