Flutter: how to force an application restart (in production mode)?

后端 未结 4 1050
無奈伤痛
無奈伤痛 2020-11-28 03:33

In production mode, is there a way to force a full restart of the application (I am not talking about a hot reload at development time!).

P

相关标签:
4条回答
  • 2020-11-28 04:15

    You can also use the runApp(new MyWidget) function to do something similar

    This is what this function does:

    Inflate the given widget and attach it to the screen.

    The widget is given constraints during layout that force it to fill the entire screen. If you wish to align your widget to one side of the screen (e.g., the top), consider using the Align widget. If you wish to center your widget, you can also use the Center widget

    Calling runApp again will detach the previous root widget from the screen and attach the given widget in its place. The new widget tree is compared against the previous widget tree and any differences are applied to the underlying render tree, similar to what happens when a StatefulWidget rebuilds after calling State.setState.

    https://docs.flutter.io/flutter/widgets/runApp.html

    0 讨论(0)
  • 2020-11-28 04:20

    The flutter_phoenix package is based on Rémi Rousselet's answer, making it even simpler.

    void main() {
      runApp(
        Phoenix(
          child: App(),
        ),
      );
    }
    

    Then when you need to restart the app, just call:

    Phoenix.rebirth(context);
    
    0 讨论(0)
  • 2020-11-28 04:21

    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.

    This would make you loose the whole state of your app.

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

    In this example you can reset your app from everywhere using RestartWidget.restartApp(context).

    0 讨论(0)
  • 2020-11-28 04:35

    I wanted a simple way to change the theme of my app (light, dark) and so i did it by using SharedPreferences package. This is my main function:

    void main() async {
        WidgetsFlutterBinding.ensureInitialized();
        final prefs = await SharedPreferences.getInstance();
    
        runApp(MyApp(prefs: prefs));
    }
    

    then I simply called this method anywhere on my app to handle the change:

    runApp(MyApp(prefs: prefs));
    
    0 讨论(0)
提交回复
热议问题