If there is only one value and no method, do we still need to use StateNotifier as follows? Or is there a simpler mechanism?
Since this value is rewritten and referenced from the outside, the corresponding function must be included in the ChangeNotifier's notifyListeners();.
class BoolNotifier extends StateNotifier<bool> {
BoolNotifier() : super(false);
}
final isA = StateNotifierProvider<BoolNotifier, bool>(
(ref) => BoolNotifier(),
);
final isB = StateNotifierProvider<BoolNotifier, bool>(
(ref) => BoolNotifier(),
);
CodePudding user response:
You may use StateProvider
:
final boolProvider = StateProvider<bool>((ref) => true);
final isA = Provider<bool>(
(ref) => ref.watch(boolProvider),
);
final isB = Provider<bool>(
(ref) => ref.watch(boolProvider),
);
You can change the state like this:
ref.read(boolProvider.notifier).update((state) => !state);
// or so
ref.read(boolProvider.notifier).state = newValue;