assertUncompletedFuture method
An async reducer (one that returns Future<AppState?>) must never complete without at least
one await, because this may result in state changes being lost. It's up to you to make sure
all code paths in the reducer pass through at least one await
.
Futures defined by async functions with no await
are called "completed futures".
It's generally easy to make sure an async reducer does not return a completed future.
In the rare case when your reducer function is complex and you are unsure that all
code paths pass through an await, there are 3 possible solutions:
-
Simplify your reducer, by applying clean-code techniques. That will make it easier for you to make sure all code paths have 'await'.
-
Add
await microtask;
to the very START of the reducer. -
Call method assertUncompletedFuture at the very END of your reduce method, right before the return. If you do that, an error will be shown in the console in case the reduce method ever returns a completed future. Note there is no other way for AsyncRedux to warn you if your reducer returned a completed future, because although the completion information exists in the
FutureImpl
class, it's not exposed. Also note, the error will be thrown asynchronously (will not stop the action from returning a state).
Implementation
void assertUncompletedFuture() {
scheduleMicrotask(() {
_completedFuture = true;
});
}