Currently, it appears not to be possible to declare a store observable that uses the late keyword. An attempt to set the observable value for the first time causes mobx to read it first throwing LateInitializationError.
class CounterStore extends _CounterStore with _$CounterStore {}
abstract class _CounterStore with Store {
@observable
late int counter;
}
The above will generate:
mixin _$CounterStore on _CounterStore, Store {
late final _$counterAtom =
Atom(name: '_CounterStore.counter', context: context);
@override
int get counter {
_$counterAtom.reportRead();
return super.counter;
}
@override
set counter(int value) {
_$counterAtom.reportWrite(value, super.counter, () { // <- reads value before the initialization has had a chance to happen
super.counter = value;
});
}
@override
String toString() {
return '''
counter: ${counter}
''';
}
}
Consider generating code in a way, that reads the current value only if the observable is not late or it's already been initialized.
Currently, it appears not to be possible to declare a store observable that uses the
late
keyword. An attempt to set the observable value for the first time causes mobx to read it first throwingLateInitializationError
.The above will generate:
Consider generating code in a way, that reads the current value only if the observable is not late or it's already been initialized.