[英]How to use method from one Change notifier class in another change notifier class provider
我想在另一個提供者方法中使用 fetchdata() 並初始化變量。
var prevProvider = Provider.of<PrevProvider>(context, listen: false);
await prevProvider.fetchdata();
希望它會起作用
如果有人仍然對此有困難,那么也許這會有所幫助:
將一個提供者方法訪問到另一個提供者中:-
class 1:-
class OneState extends ChangeNotifier {
List<Strings> _names = ["Alex", "Brad", "Carol"];
List<GlobalKey> get names=> _names;
updateNames(
{required List<Strings> names}) {
_names = names;
notifyListners();
}
}
class 2:-
class AnotherState extends ChangeNotifier {
AnotherState({required this.watchOneStateProviderValue}); //<- Notice this.
OneState watchOneStateProviderValue; //<- Notice this.
late List<int> _iq;
List<GlobalKey> get iq=> _iq;
createIQFromNames() {
// Getting names from another provider.
List<String> names = watchOneStateProviderValue.names; //<- Notice this.
_iq = [];
for(var name in names){
if(name == "Park Ju-hyun"){
iq.add(99999);
} else {
iq.add(0);
}
}
notifyListners();
}
}
現在像這樣聲明提供者:-
final oneStateProvider =
ChangeNotifierProvider((ref) => OneState());
final anotherStateProvider = ChangeNotifierProvider((ref) {
// We use `ref.watch` to watch another provider, and we pass it the provider
// that we want to consume. Here: oneStateProvider
final CalendarPropertiesState watchCalendarPropertiesStateProviderValue =
ref.watch(calendarPropertiesStateProvider);
// We can then use the result to do something based on the value of `oneStateProvider`.
return AnotherState(watchOneStateProviderValue: watchOneStateProviderValue);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.