0

I've been developing in Flutter for a few months and I'm not yet very experienced. These days I'm working on an app that I didn't create from the beginning and I'm having a strange problem, unfortunately I can't paste too much pieces of code but I try to explain the wrong behavior. The state of the app is contained in an InheritedWidget that is called before all the others. For example, in this InheritedWidget there is a value that must always be visible at the top of the app (in the AppBar). The problem is that if at runtime this value is changed in the InheritedWidget, the view shows the previous value (as if it wasn't updated), but if I do Navigator.push() to a new page, the AppBar shows the correct value (i.e. the updated one). If I pop to the previous page, the old value reappears in the AppBar. If I put the app in the background and bring it back to the foreground, the correct value finally appears.

It seems that the view does not update even if the value changes in the InheritedWidget. I specify that before being displayed, this value is extracted directly from the InheritedWidget using context.dependOnInheritedWidgetOfExactType<InheritedWidgetName>(). I also specify that "updateShouldNotify" is set this way: @override bool updateShouldNotify(Session oldWidget) { return true; }

I wanted to ask if anyone knows what might be causing this problem.

Thanks in advance.

  • The solution is, get the call of that updating value function in "init state". The logic is that whenever the entire widget tree will build the "init state" will get called, and your value will be setup. When app goes in background and you come back in the foreground, the entire widget tree builds again. The solution works for me perfectly, and hopefully, it will work for you as well. :) – Sanket Vekariya Apr 04 '20 at 11:40
  • Sorry, I didn't quite understand. By "init state" do you mean the "initState()" method that you can overrides inside stateful widgets? And what should I call up in there? The function that finds the value from inheritedwidget or the function that changes the value? I specify that the function that changes the value is called from a widget other than the one in which it is displayed. Anyway, thanks for your help :) – antoniocapizzi95 Apr 04 '20 at 13:32
  • My funda is that when you set the values in the first screen from where you are passing the data, you should set the state, here it seems the state is not be set. that's why after coming back from the second screen the change is not been reflected. If you had set the values in init state, then you won't need to bother about all the things. I guess debugging after calling navigator.pop() method will surely let you know how the data fill in the first screen. – Sanket Vekariya Apr 04 '20 at 13:48
  • The problem is no state is set, the value I have to display comes directly from the inheritedwidget – antoniocapizzi95 Apr 04 '20 at 14:39

0 Answers0