2

My app uses server responses when generating(building) different screens. I get the server responses at initState.

Re-building the screen(s) by setState will not trigger the initState. It will only update the state by setState input and re-build the widget.

Some actions in the app require fully restarting the app(for fetching all latest server responses and rebuilding the UI).

Rebuilding the whole app by

runApp(new MyApp()); (, or rebuilding all screens by setState of root screen) is fine to refresh the UI widgets. However, it cannot refresh all screens' state which holds the server responses.

e.g.

// MyState

String serverResponse;

@override
void initState() {
  super.initState();
  serverResponse = await http.get(url....
}

@override
Widget build(BuildContext context) {
  return new Text(serverResponse);
}

App users can open many similar screens at the same time. When they change the state which will globally affect all screens. I cannot do

setState(() {
  serverResponse = await http.get(url....
});

for every opened screens when I want to reset the whole app.

I mainly have two questions.

  1. If I want to keep the opened screens and their states. May I know is it possible to do something like flutter markneedsbuild? Instead of triggering the build only, I would like to trigger both initState and build when the screen is visited again. Does it violate the flutter design/flow?

  2. If thing does not work like this, I think a simple way should be discarding all the previous screens and states. Everything starts over again. Is there any way to discard the old MyApp and re-create a new MyApp? So that all the states can be initialized again.

Many thanks for your reading and any of help!

h19881103
  • 373
  • 2
  • 3
  • 9

0 Answers0