Two ways to restart Flutter App programmatically
In general, there are limited scenarios where we would need to restart a Flutter app programmatically. Restarting the app is not a common practice, and most apps do not require this functionality. However, there are a few specific cases where it might be useful.
Use cases of restart application
1- Resetting App State: Sometimes, we might want to provide a “reset” feature in our app that allows users to clear all the current app state and start fresh. In such cases, we could use a restart to achieve this effect.
2- Handling Configuration Changes: On some devices, when configuration changes occur (e.g., changing the device’s language, theme, or orientation), Flutter’s hot-reload or hot-restart might not trigger the necessary updates in the UI. In these rare situations, a full restart might be necessary to apply the configuration changes properly.
3- App Update/Upgrade: When an app update or upgrade occurs, we might need to restart the app to ensure all new changes are properly applied and the app functions as intended.
4- Debugging and Testing: While developing/debugging an app, especially when dealing with complex state management, restarting the app can be a quick way to reset everything and test different app scenarios. It’s essential to use app restarts judiciously since restarting the app will clear all the current state, which may lead to a suboptimal user experience. In most cases, Flutter’s built-in hot-reload or hot-restart should suffice for making updates during development
Restart Flutter App Programmatically
Today we will see the two methods by which we can restart our Flutter app programmatically. First method uses the Flutter’s built in methodology to restart the application. And second method is very easy, it requires a third party library, which allows us to restart application only with one live of code.
Let me demonstrate the first method. Here is the full code and explanation of code.
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,
);
}
}
This code demonstrates how to implement a simple RestartWidget in Flutter that allows us to restart our app programmatically. It uses Flutter’s Key and KeyedSubtree widgets to achieve the restart effect.
Let’s break down the code step by step:
1- The main() function:
In the main() function, the app starts with the RestartWidget as its root widget wrapped around a MaterialApp. The RestartWidget acts as a wrapper for the entire app and provides the ability to restart the app from any part of the widget tree.
2- The RestartWidget class:
RestartWidget is a StatefulWidget that takes a child parameter in its constructor. The child is the widget subtree that the RestartWidget will wrap around and manage.
It contains a static method named restartApp(BuildContext context). This method allows us to restart the app programmatically from anywhere in the app by calling RestartWidget.restartApp(context).
The _RestartWidgetState class:
3- _RestartWidgetState is the state of the RestartWidget.
It has a Key named key, which is initially set to a UniqueKey(). This key is crucial as it allows the KeyedSubtree to determine whether to rebuild its subtree.
The restartApp() method is responsible for restarting the app. It sets a new UniqueKey to the key, which forces a rebuild of the widget tree wrapped by the KeyedSubtree. As a result, the entire app is rebuilt, effectively restarting it.
4- The build() method of _RestartWidgetState:
In the build() method, the RestartWidget wraps its child widget with a KeyedSubtree.
With this implementation, we can use the RestartWidget.restartApp(context) method from anywhere in our app (by providing the appropriate BuildContext) to trigger a full restart of the app.
2nd Method to restart Flutter App Programmatically
This method required a package Flutter Phoenix. This allows us to easily restart our application from scratch, losing any previous state.
Add the package as a dependency in your pubspec.yaml file.
We just have to wrap our App widget in phoenix widget, like this
void main() {
runApp(
Phoenix(
child: App(),
),
);
}
We will call the rebirth static method when we want to restart our application (rebuild the entire widget tree from scratch).
Phoenix.rebirth(context);