By Alvin Alexander. Last updated: January 4, 2022
Sometimes a Dart or Flutter API may require you to return a Future
even if you already have a known, constant, or literal value. If/when that happens, you can use this approach:
return new Future(() { return 42; });
In this example the known value is 42
, but it could have been a string like "Hello"
, a boolean like true
, or any other known or literal value.
I was reminded of this when I saw the following Dart Future/delayed code, and asked why it was written the way it was:
return Future.delayed(Duration(milliseconds: 0))
.then((onValue) => 42);