Want to force an application restart in production mode in Flutter? You could wrap your whole app into a statefulwidget. And when you want to restart you app, rebuild that statefulwidget with a child that possess a different Key.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import 'package:flutter/material.dart'; void main() { runApp( RestartWidget( child: MaterialApp(), ), ); } class RestartWidget extends StatefulWidget { RestartWidget({this.child}); final Widget child; static void restartApp(BuildContext context) { context.findAncestorStateOfType<_RestartWidgetState>().restartApp(); } @override _RestartWidgetState createState() => _RestartWidgetState(); } class _RestartWidgetState extends State<RestartWidget> { Key key = UniqueKey(); void restartApp() { setState(() { key = UniqueKey(); }); } @override Widget build(BuildContext context) { return KeyedSubtree( key: key, child: widget.child, ); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.