You cannot avoid asynchrony.
In Dart, a function returns a value or Future
. It returns the future when the result is not ready yet, when the function returns, it will only be available later.
This means you're stuck if all you have is this Future
and you want to get the value right now, because the value just doesn't exist yet. Now you cannot return List
, because Future<List>
now has no list, he has it later.
So, you must wait for this future, var data = await client.getUrl(...)...
which means that your function must be asynchronous as well. So, run getList
function a Future<List>
instead List
, because that's all it can do.
An alternative that Dart does not support would be a blocking operation that stops execution until a value is available and then resumes normal execution. This is a problem in a single threaded language (like Dart and JavaScript that Dart has to compile) as blocking means nothing happens and the UI becomes unresponsive.
So, if your result depends on the value of the future, your function should also be asynchronous and return its own result to the future.
source
to share