Tugay
Tugay

Reputation: 2214

Calling notifyListeners() causes errors when called in deactivate() - Flutter

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?

Generally, is there a way to call notifyListeners() when I leave page?

Upvotes: 1

Views: 901

Answers (1)

Damian K. Bast
Damian K. Bast

Reputation: 1204

This could be that you are overwriting the wrong function:

In general I would advise you to have a look at navigation, maybe a simple "onPopPage" at the place where you are routing to your page could be what you are looking for.

Upvotes: 1

Related Questions