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.