Home > Mobile >  How to restart Flutter app on android without closing and opening the app again?
How to restart Flutter app on android without closing and opening the app again?

Time:07-08

How to restart Flutter app on android without closing and opening the app again?

i use these MyApp.restartApp() and SystemNavigator.pop()

but the problem is that it is not user friendly, me too as i develope the application closing and re opening the app is bit anoying.

i dont know the proper term on what my point is but you may get my idea.

CodePudding user response:

Try this: I use this when i need to restart app for example internet is restored after being down

in your main

runApp(const RestartWidget(child: const MyApp()));

create restart widget


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,
    );
  }
}

then in your main add this

runApp(const RestartWidget(child: const MyApp()));

Then to trigger restart say in a button

onTap: (){
    RestartWidget.restartApp(context);
               },

CodePudding user response:

If you're in your IDE, while your app is running you can try to perfom a Hot Restart of a Hot Reload by pressing r or R keys.

  • Related