Tuesday 10 November 2020

setState() or markNeedsBuild() called during build error - when calling notifyListeners() on deactivate

I have read all the questions related to this issue, but neither of them is similar to mine.

I have two pages: homepage and settings page. In the homepage, I listen to changes of provider with Consumer. In the settings page, I don't listen to changes:

class _SettingsState extends State<Settings> {
    StatesProvider states;

    @override
    void initState() {
        states =  Provider.of<StatesProvider>(context, listen: false);
        super.initState();
    }
    @override
    Widget build(BuildContext context) {
        return myUi();
    }
     

    @override
    void deactivate() {
        states.changeSettings(settings); //just a dummy function, doesn't do anything  
        states.finishSetting();
        super.deactivate();
    }

}

I have cut out unnecessary codes. What I am trying to do in code above is changing some app settings and applying them when I leave that settings page.

finishSetting() method:

finishSetting() {
    notifyListeners();
  }

Error:

The following assertion was thrown while dispatching notifications for StatesProvider:
setState() or markNeedsBuild() called during build.

I know that notifyListeners() calls build() method of Widgets listening to it. But when I leave the settings page, I don't see any widget's build() method called. What should I do?



from setState() or markNeedsBuild() called during build error - when calling notifyListeners() on deactivate

No comments:

Post a Comment