API Reference
ScopedDependy
Provides two methods for accessing dependencies from a scope:
dependy<T>(): Resolves a dependency of typeT. Does not listen for changes.watchDependy<T>(): Resolves aChangeNotifierof typeTand listens for changes. Triggers a rebuild when the notifier updates.
getDependyScope
Retrieves the nearest ScopedDependy from the widget tree:
final scope = getDependyScope(context);
final state = await scope.watchDependy<MyState>();Rebuild scope
When using getDependyScope with watchDependy, the rebuild happens at the widget that provided the scope. Use ScopedDependyConsumer to limit rebuilds to a smaller subtree.
Widgets and Mixins
ScopedDependyMixin
A mixin for StatefulWidget that provides scoped dependencies.
Methods:
dependy<T>(): Resolve without listeningwatchDependy<T>(): Resolve and listen for changesparentModule(): Access the parent scope's moduleshareDependyScope(): Share this scope with descendantsmoduleBuilder(): Override to return the scopedDependyModule
class _MyState extends State<MyWidget> with ScopedDependyMixin {
@override
Widget build(BuildContext context) {
return shareDependyScope(
child: MyChildWidget(),
);
}
@override
DependyModule moduleBuilder() {
return DependyModule(
providers: { /* ... */ },
modules: {parentModule()},
);
}
}WARNING
Do not return a singleton module from moduleBuilder(). That module will be disposed when the widget is removed from the tree. Include singletons as submodules instead.
ScopedDependyProvider
A widget that provides scoped dependencies. Works with both StatelessWidget and StatefulWidget.
Parameters:
builder: Builds the widget with access toScopedDependyshareScope: Whentrue, descendants can access this scopemoduleBuilder: Returns a scopedDependyModule
ScopedDependyProvider(
shareScope: true,
moduleBuilder: (parentModule) => DependyModule(
providers: { /* ... */ },
modules: {parentModule()},
),
builder: (context, scope) => MyWidget(),
);ScopedDependyConsumer
Consumes dependencies from the nearest scope. Limits rebuilds to just this widget instead of the entire scope provider.
ScopedDependyConsumer(
builder: (context, scope) {
return FutureBuilder(
future: scope.watchDependy<CounterService>(),
builder: (context, snapshot) => Text('${snapshot.data?.counter}'),
);
},
);ScopedDependyAsyncBuilder
Resolves an async dependency and provides a sealed AsyncDependySnapshot<T>, replacing the common ScopedDependyConsumer + FutureBuilder nesting pattern.
When the resolved type is a ChangeNotifier, it automatically listens for changes (controlled via watch, which defaults to true).
Snapshot types (exhaustive pattern matching):
AsyncDependyLoading: resolving in progressAsyncDependyData: resolved successfullyAsyncDependyError: resolution failed
ScopedDependyAsyncBuilder<CounterService>(
builder: (context, snapshot) => switch (snapshot) {
AsyncDependyData(:final value) => Text('${value.counter}'),
AsyncDependyError(:final error) => Text('Error: $error'),
AsyncDependyLoading() => const CircularProgressIndicator(),
},
);Optional parameters:
module: resolve from a specific module instead of the widget tree scopetag: resolve a tagged providerwatch: disable automaticChangeNotifierlistening (default:true)
DependyNotifierListener
Listens to a ChangeNotifier and rebuilds when it updates. Used with EagerDependyModule where scoped widgets are not needed.
final counterService = dependy<CounterService>();
DependyNotifierListener(
notifier: counterService,
builder: (context, notifier) => Text('${counterService.counter}'),
);