Closed jamiewest closed 4 years ago
This is not how you should use StateNotifier
.
You should not have state that needs to be listened, that is not part of the state
variable.
Instead do:
class Person {
String email = '';
String name = '';
}
class PersonController extends StateNotifier<Person> {
PersonController(): super(Person());
set email(String value) {
state = Person()
..email = value
..name = state.name;
}
}
Was this a design choice to have the State
and StateNotifier
be to separate components or was due to an engineering requirement in order to make things work with provider?
This is a design choice. StateNotifier is not ChangeNotifier, but ValueNotifier
I see, that is probably where I am confusing things. Thank you.
Not sure if this is against the intended design, but if you're looking for something like ChangeNotifier but decoupled from Flutter, you can accomplish something like it as so:
abstract class StateChangeNotifier extends StateNotifier<void> {
StateChangeNotifier() : super(null);
void notifyListeners() => state = null;
}
I wouldn't recommend this is in many cases because StateNotifier
forces you to separate the state into its own object which ends up with cleaner code.
I think it would be helpful to have a
notifyListeners
method on theStateNotifier
class to more closely resemble aChangeNotifier
.This would go along with using
StateNotifier
in manner similar toChangeNotifier
.