I want to use fetchdata() in another provider method and initialise variables.
Asked
Active
Viewed 1,858 times
1

General Grievance
- 4,555
- 31
- 31
- 45

Tarun siva sai
- 21
- 4
-
Hi! You'd better to post your code as markdown code block instead images. See how-to-ask https://stackoverflow.com/help/how-to-ask – Mol0ko May 24 '21 at 07:20
-
i have the same problem.. how did you solve it? – Harald Wiesinger Nov 10 '21 at 12:45
3 Answers
3
you can use MultiProvider with ChangeNotifierProxyProvider
Action Class ( FirstModel )
class FirstModel with ChangeNotifier { List<Strings> _names = ["Sat", "Sat2", "Sat3"]; List<Strings> get names { return _names ; } }
Action Class ( SecondModel )
class SecondModel with ChangeNotifier { SecondModel(this.firstModel); final FirstModel firstModel; List<Strings> getNames(){ return firstModel.names; } }
In main.dart just update the Multiprovider, example below
void main() {
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider<FirstModel>(create: (_) => FirstModel()),
ChangeNotifierProxyProvider0<SecondModel>(
create: (BuildContext context) =>
SecondModel(Provider.of<FirstModel>(context, listen: false)),
update: (BuildContext context, SecondModel secondModel) =>
SecondModel(Provider.of<FirstModel>(context, listen: false)),
),
],
child: MyApp(),
),
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: MyHomePage(),
);
}
}
Similar other class also avilable... For More Information please refer below link...

sathish
- 300
- 3
- 14
0
If someone still has difficulties with this then maybe this will help:
Accessing one provider method into another provider:-
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();
}
}
Now declare providers like this:-
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);
});

Deepanshu
- 890
- 5
- 18
-1
var prevProvider = Provider.of<PrevProvider>(context, listen: false);
await prevProvider.fetchdata();
Hope it will work

Saied Islam Shuvo
- 202
- 1
- 7
-
4it says it needs context, how do I provide a context if I'm using this code inside a class – Tarun siva sai May 24 '21 at 23:56