While using shared prefs plugin it's common to explore code like below
void saveList() async {
final prefs = await SharedPreferences.getInstance();
prefs.setStringList("listKey", aList); //await isn't used
}
where setStringList returns a Future<bool> so that why in this case the await isn't used.
awaitisn't used in your example because it wouldn't do anything and therefore would be unnecessary.The purpose of
awaitis to make subsequent code wait for theFutureto complete. That is, if you have:then your program would wait for
foo()to asynchronously complete before invokingbar().If you have:
that's implicitly equivalent to:
and your program would wait for
foo()to asynchronously complete before "returning" frombaz(). ("Returning" from an asynchronous function actually means completing the returnedFuture.)In your example:
There is nothing left to do after
prefs.setStringListcompletes. There is no code afterward that depends on its completion, neither insaveListitself nor insaveList's callers. (SincesaveListreturnsvoidinstead of aFuture, it is a "fire-and-forget" function; callers cannot wait for it to complete even if they wanted to.) Therefore, usingawait prefs.setStringList(...)wouldn't change any behavior and wouldn't serve any purpose.