AppRestarter class

A StatefulWidget that enables soft app restart by rebuilding its subtree.

Place AppRestarter near the root of your widget tree (above MaterialApp):

runApp(AppRestarter(child: MyApp()));

Then call AppRestarter.restart from anywhere in the tree to trigger a full rebuild:

AppRestarter.restart(context);

Re-initialising services on restart

Use onRestart to run async work (e.g. re-initialising Sentry or Dio) before the widget tree rebuilds. The new environment values are already active when onRestart fires, so calls to Env.get(...) return the switched config:

AppRestarter(
  onRestart: () async {
    await Sentry.close();
    await SentryFlutter.init(options: ..dsn = Env.get('SENTRY_DSN'));
  },
  child: SentryWidget(child: MyApp()),
)

Dynamic child re-creation

When the widget itself (e.g. a GoRouter) must be rebuilt with a fresh reference on every restart, use builder instead of child. The builder is called anew each time a restart occurs:

AppRestarter(
  onRestart: () async => router = AppRouter.create(),
  builder: (ctx) => MyApp(router: router),
)

Exactly one of child or builder must be provided.

Inheritance

Constructors

AppRestarter({Key? key, Widget? child, WidgetBuilder? builder, Future<void> onRestart()?})
Creates an AppRestarter with a static child.
const

Properties

builder WidgetBuilder?
A builder called on every restart to produce the subtree.
final
child Widget?
The subtree to rebuild on restart.
final
hashCode int
The hash code for this object.
no setterinherited
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
onRestart Future<void> Function()?
Optional async callback invoked before the widget tree rebuilds.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

createElement() StatefulElement
Creates a StatefulElement to manage this widget's location in the tree.
inherited
createState() State<AppRestarter>
Creates the mutable state for this widget at a given location in the tree.
override
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) String
A string representation of this object.
inherited
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug, int wrapWidth = 65}) String
Returns a string representation of this node and its descendants.
inherited
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object.
inherited
toStringShort() String
A short, textual description of this widget.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

restart(BuildContext context) → void
Triggers a full rebuild of the nearest AppRestarter ancestor.