1

I'm using the technique from this SO answer to "restart" my app. The problem is that the "home" page of my MaterialApp doesn't get recreated (it's initState is not executed on restart).

For this to happen, I have to briefly return something else than the MaterialApp from the build method in my app state.

class _RestartWidgetState extends State<RestartWidget> {
  Key key = new UniqueKey();

  static bool isRestarting = false;

  void restartApp() {
    this.setState(() {
      isRestarting = true;
      key = new UniqueKey();
    });

    Timer(Duration(milliseconds: 500), (){
      this.setState(() {
        isRestarting = false;
        key = new UniqueKey();
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Container(
      key: key,
      child: widget.child,
    );
  }
}


class MyAppState extends State<MyApp>{

  @override
  Widget build(BuildContext context) {
    if( _RestartWidgetState.isRestarting ) {
      return Container( ... );
    }
    else{
      return ScopedModel<MyModel>(
        model: myModel,
        child: MaterialApp(
          home: MainScreen(),

          ...

        )
      );
    }
  }
}

Is there another/better way of doing this? It feels kind of sketchy.

Magnus
  • 17,157
  • 19
  • 104
  • 189
  • Your RestartWidget is placed above MyApp right? – Rémi Rousselet Sep 25 '19 at 13:52
  • @RémiRousselet Correct. `MyAppState.initState` is executed, but `MainScreenState.initState` is not, unless I briefly switch out the entire layout for some other widget (the `Container( ... )`) and then back to the `MaterialApp`. – Magnus Sep 25 '19 at 17:59

0 Answers0