ModuleScope<T extends Module> class

Widget that manages the lifecycle of a Module and exposes its DI container.

ModuleScope creates a ModuleController, initializes the module, and provides the resulting Binder to descendants via ModuleProvider. It handles loading and error states and supports configurable retention policies that control when the controller is disposed or reused.

Basic Usage

ModuleScope<AuthModule>(
  module: AuthModule(),
  child: const AuthPage(),
)

Retention Policy

Controls how the ModuleController is retained across widget rebuilds:

Retention Key vs Override Scope

Important distinction:

Two scopes with the same retentionKey but different overrideScopes will share the same cached controller (first scope's overrides win).

To make overrides affect caching, include scope identity in the key:

ModuleScope(
  module: MyModule(),
  retentionPolicy: ModuleRetentionPolicy.keepAlive,
  retentionKey: 'my-module-${identityHashCode(overrideScope)}',
  overrideScope: overrideScope,
  child: ...,
)

See also:

Inheritance

Constructors

ModuleScope({Key? key, required T module, required Widget child, dynamic args, WidgetBuilder? loadingBuilder, Widget errorBuilder(BuildContext, Object? error, VoidCallback retry)?, ModuleRetentionPolicy retentionPolicy = ModuleRetentionPolicy.routeBound, Object? retentionKey, Map<String, Object?>? retentionExtras, void overrides(Binder)?, ModuleOverrideScope? overrideScope})
Create a ModuleScope that manages the lifecycle of module.
const

Properties

args → dynamic
Arguments passed to Configurable.configure if module implements it.
final
child Widget
Widget subtree that can access the module's DI container.
final
errorBuilder Widget Function(BuildContext, Object? error, VoidCallback retry)?
Builder for error state UI with retry callback.
final
hashCode int
The hash code for this object.
no setterinherited
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
loadingBuilder WidgetBuilder?
Builder for loading state UI.
final
module → T
The module instance to manage.
final
overrides → void Function(Binder)?
Overrides applied to module's bindings.
final
overrideScope ModuleOverrideScope?
Override scope tree for this module and its imports.
final
retentionExtras Map<String, Object?>?
Additional data for retention key derivation.
final
retentionKey Object?
Explicit key for KeepAlive cache identity.
final
retentionPolicy ModuleRetentionPolicy
How the controller lifecycle is managed.
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<ModuleScope<T>>
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