1

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?

Tugay
  • 2,057
  • 5
  • 17
  • 32

1 Answers1

1

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.

Damian K. Bast
  • 1,074
  • 7
  • 18
  • Well, `dispose()` gives the same error and I don't know how to implement `onPopPage` – Tugay Nov 10 '20 at 20:57
  • I analyzed navigation and saw that I gotta change a lot more things to get it work. It works now and without you, it wouldn't have been possible. I will award bounty to you, but won't accept answer, because it really didn't answer my question. I hope you understand. – Tugay Nov 10 '20 at 22:18