How can I return Future<void>
?
Future<void> deleteAll(List stuff){
stuff.forEach( s => delete(s)); //How do I return Future<void> when loop and all delete Operation are finished?
}
Future<void> delete(Stuff s) async {
....
file.writeAsString(jsonEncode(...));
}
How do I return Future<void>
when the forEach
loop and all delete Operations are finished?
You don't need to return anything manually, since an async
function will only return when the function is actually done.
Looking at your examples you are missing the async
keyword, which means you need to write the following instead:
Future<void> deleteAll(List stuff) async {
stuff.forEach( s => delete(s));
}
Future<void> delete(Stuff s) async {
....
await file.writeAsString(jsonEncode(...));
}
There is no need to return anything, since void
is nothing, as the name implies.
Also make sure you call deleteAll
and writeAsString()
using await
.
I want to get notified, when deleteAll and all delete calls are finished, so that I can use delteAll().then(() => refreshUI()). What would be the correct strategy for that?
To use
.then()
, use my code example. Defining the function signature asasync
is enough.Okay, thank you :)
Also note that calling
deleteAll()
and belowrefreshUI()
is essentially the same sincedelete()
is not an asynchronous function.delete() is an async function. I forgot to add the async keyword in the code snippet. The implementation of delete() is using getApplicationsDIrectory() which is also an async function.