I would like to know if there is any way to call a function on only the starting of the app, but before building the widget. I would like to fetch data from the JSON only on starting of the app. But the Method is in the provider and needs context.
I tried several ways but nothing is perfect. I called the function on initState of the HomeScreen() but still need to refresh the app to see. (because the method is called after the layout is built.. so no items loaded)
void initState() {
super.initState();
Provider.of<Utils>(context, listen: false)
.fetchDataFromJSON()
.then((value) {
setState(() {});
});
Still need refresh or change page to load the items. I am doing this initState in tabScreen.dart. Obviously, the data needed for creating the Elements on HomeScreen but its not loading at first time.
EDIT: I got it fixed by changing the place of calling the function. My Mistake is I called the init state from the TabScreen's InitState. I changed it to HomeScreen's InitState and it worked fine. Hope my mistake will help someone else.
notifyListeners()after fetching as the function is inProvider. Alternatively ininitStateyou can dofetchData().then( (_) => setState(() {}) );- Navaneeth P