In Javascript you can convert a callback to a promise with:
function timeout(time){
return new Promise(resolve=>{
setTimeout(()=>{
resolve('done with timeout');
}, time)
});
}
Is that possible in Flutter?
Example:
// I'd like to use await syntax, so I make this return a future
Future<void> _doSomething() async {
// I'm call a function I don't control that uses callbacks
// I want to convert it to async/await syntax (Future)
SchedulerBinding.instance.addPostFrameCallback((_) async {
// I want to do stuff in here and have the return of
// `_doSomething` await it
await _doSomethingElse();
});
}
await _doSomething();
// This will currently finish before _doSomethingElse does.
2
Answers
Found the answer from this post.
So to accommodate the example listed above:
saying that we have a normal method that return just a value like this:
we can make it a
Future
by assigning it directly toFuture.value()
like this: