如果只有一個值,沒有方法,我們還需要像下面這樣使用StateNotifier嗎?還是有更簡單的機制?
由于這個值是從外部重寫和參考的,因此相應的函式必須包含在 ChangeNotifier 的 notifyListeners(); 中。
class BoolNotifier extends StateNotifier<bool> {
BoolNotifier() : super(false);
}
final isA = StateNotifierProvider<BoolNotifier, bool>(
(ref) => BoolNotifier(),
);
final isB = StateNotifierProvider<BoolNotifier, bool>(
(ref) => BoolNotifier(),
);
uj5u.com熱心網友回復:
您可以使用StateProvider:
final boolProvider = StateProvider<bool>((ref) => true);
final isA = Provider<bool>(
(ref) => ref.watch(boolProvider),
);
final isB = Provider<bool>(
(ref) => ref.watch(boolProvider),
);
您可以像這樣更改狀態:
ref.read(boolProvider.notifier).update((state) => !state);
// or so
ref.read(boolProvider.notifier).state = newValue;
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/520888.html
上一篇:如何制作可移動的疊加層?
