[etos_flutter] Make StateBuilder more 'null-safe'
Closed this issue · 1 comments
SEGVeenstra commented
Sometimes a page is only shown when the app is in a specific state.
But the StateBuilder does not know this so therefor you have to do some 'pointless' checks for example:
Let's say the ItemsPage is only shown when the user is logged in, our AppState is a Union type that can be either Authenticated
or Unauthenticated
. It only contains items when being Authenticated
.
A StateBuilder could look like this:
StateBuilder<AppState, List<Item>>(
//option 1:
converter: (state) => state.tryCast<Authenticated>()?.items ?? [], // We have to provide a default value or make the type nullable
// option 2:
converter: (state) => state.cast<Authenticated>().items, // This throws an error when the app is not `Authenticated`.
builder: (_, items) {
...
}
SEGVeenstra commented
This might actually benefit from a buildWhen
method like in flutter_bloc