Dart: How to return Future

You don’t need to return anything manually, since an async function will only return when the function is actually done, but it depends on how/if you wait for invocations you do in this function.

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(...));
}

When using Future<void> there is no need to explicitly return anything, since void is nothing, as the name implies.

Also make sure you call deleteAll and writeAsString() using await.

Note: To wait for all delete/foreach invocations to complete, see below answer for more details. In short you will need to put all delete invocations in a Future.wait for that.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)