What I did (to present loading state example) is:
@freezed
abstract class CounterState with _$CounterState {
const factory CounterState.initial([@Default(0) int value]) = _Initial;
const factory CounterState.loading(int value) = _Loading;
const factory CounterState.current(int value) = _Current;
}
class CounterStateNotifier extends StateNotifier<CounterState>
with LocatorMixin {
CounterStateNotifier() : super(CounterState.initial());
void increment() {
state = CounterState.loading(state.value);
Timer(
Duration(seconds: 1),
() => state = CounterState.current(state.value + 1),
);
}
}
StateNotifierBuilder(
stateNotifier: context.watch<CounterStateNotifier>(),
builder: (context, CounterState state, _) => state.when(
initial: (value) => Text(
'$value',
style: Theme.of(context).textTheme.headline4,
),
loading: (_) => CircularProgressIndicator(),
current: (value) => Text(
'$value',
style: Theme.of(context).textTheme.headline4,
),
),
)
But that is obviously 'hack'. Any tip? Remi Rousselet / Ryan Edge