Consuming immediately available async results in one frame #113763
Labels
c: proposal
A detailed proposal for a change to Flutter
dependency: dart
Dart team may need to help us
framework
flutter/packages/flutter repository. See also f: labels.
P3
Issues that are less important to the Flutter project
team-framework
Owned by Framework team
triaged-framework
Triaged by Framework team
This issue is about the problem of consuming immediately available results from a
Future
within one frame.A typical use case is a data API that caches results of previous requests.
Sometimes a result can be resolved synchronously (e.g. from a cache) or asynchronously (e.g. from the network). The producer of such a result can create and hand out a
Future
synchronously and asynchronously. Only async code can consume aFuture
, though.Code that runs as part of building widgets must be synchronous and cannot consume an immediately available result that is contained in a
Future
in the same frame that awaits theFuture
. The result is only accessible in the next frame, creating an unnecessary delay or worse a flicker in the UI.SynchronousFuture
SynchronousFuture
calls itsthen
callback synchronously, and thus allows its result to be consumed synchronously.The name
SynchronousFuture
is somewhat distracting. We don’t necessarily care about the result being available synchronously. We care about the result being visible in the same frame.Problems with
SynchronousFuture
Future
contract. It happens to work with the current Dartawait
implementation, but that is not guaranteed.SynchronousFuture
only when you're the only one ever callingthen
on it”Possible solutions
Custom result type
A custom result type can expose an API to access the result as soon as it is available. The downside of this solution is that it excludes all the useful affordances available for working with async computations such as
await
, standard library APIs and packages that build on async Dart APIs.Multiple build/layout phases per frame
After building dirty widgets, allow microtasks that were scheduled to run, and build widgets that were dirtied by those microtasks. This needs to be repeated in a loop so that all widgets (including those that were rebuilt in another than the first build phase) can consume immediately available results.
In a loop:
Upsides
await
and the standard library works as expected.FutureOr
/ other custom result type.Downsides
Change Dart to somehow support synchronous
Future
sAs discussed in the two issues above, there are lots of problems with making that work.
The text was updated successfully, but these errors were encountered: