Flutter
Cached feature scopes
Retain feature data, own view objects separately, and handle resolver readiness and revocation.
Resolver-only access
Use context.diResolver in build when replacement of the nearest resolver
boundary should rebuild the consumer. Use context.readDiResolver for one-off
event handlers. Their optional counterparts are maybeDiResolver and
maybeReadDiResolver.
DiResolverScope(resolver: source, child: ...) exposes resolution without
giving descendants container administration. It does not own or close the source.
At this boundary context.di and readDi throw
DiContainerAccessDeniedException; optional container lookups return null.
An unavailable inner resolver boundary never falls back to an outer root.
These accessors follow scope identity, not changes inside business objects.
Retain a local feature between screen visits
Use an ordinary DiScope when leaving the feature should close its graph.
Use DiCachedScope<A> when its managed data/state should survive temporary
removal from the widget tree without becoming global.
The application/session owner creates a DiScopeCache with explicit capacity
and idle TTL. It also creates stable DiScopeDefinition<A> values. Pass the
same definition and immutable arguments on reopening. Do not create a new
definition inside every build: definition identity is part of the cache key.
DiCachedScope<int>(
cache: cache,
definition: cartDefinition,
viewDefinition: screenDefinition,
arguments: 7,
onCloseError: reportCloseError,
builder: (
final BuildContext context,
final AsyncSnapshot<DiResolver> snapshot,
) {
if (snapshot.hasError) {
return const Text('Cart unavailable');
}
return snapshot.hasData
? const CartScreen()
: const Text('Preparing cart');
},
)
Here cartDefinition describes retained feature data, while optional
screenDefinition describes a fresh owned child for that particular screen.
Include every public type/key used by the screen in its expose allowlist,
including permitted inherited registrations.
The builder runs under a resolver-only boundary. snapshot.data and
context.readDiResolver provide its resolver; context.readDi intentionally
does not. Put dependency reads below the ready state, not before it.
Handle waiting and error states instead of treating every frame as ready.
What closes, and what remains
- Mounting acquires a lease of the cached feature. A ready hit has no artificial waiting frame; cold acquisition can wait.
- With
viewDefinition, mounting creates a separate noncached owned child. Put screen controllers there, not in retained feature state. - Unmounting revokes screen access, closes the view child and releases the lease. It does not immediately destroy a feature another lease still uses.
- Once unused, the feature remains until idle TTL or eviction. Reopening can reuse the same managed instances and state.
- The owning session closes/invalidate its cache on logout or account changes. Closing the root also closes its owned caches. GC is not the cleanup policy.
Changed cache, definition identity or argument value ends the old access and
selects the new feature. Changing only viewDefinition recreates the view child,
not a still-active feature. Late acquire completions after unmount are released;
they are not published to a removed screen. Invalidation produces an unavailable
state rather than silently reacquiring privileged access.
Supply onCloseError for teardown failures and onRollbackError for secondary
view-setup cleanup failures. Flutter cannot await widget disposal; the cache/root
owner can await final close. Retention does not pause network work, refresh data,
persist across process restarts or automatically rebuild UI when data changes.
The complete cached-cart application
owns its root/cache/definitions, preserves an item count across close/reopen,
and recreates/disposes its screen controller separately. It uses
ValueListenableBuilder to observe the counter; DI itself is not the observer.
The cart uses the memory-pressure API available since 1.3. Use the example
source as lib/main.dart in a Flutter application with the dependencies above and run
flutter run there. This package directory has no platform runner projects.
For exact cache ownership, capacity and eviction rules see
retained scopes.
For typed modules, runtime/cache snapshots, setup rollback and the separate
ark_di_test fixtures see
composition and diagnostics.
Memory pressure
Since 1.3, the owner can opt into releasing idle graphs on memory pressure. Active leases and view pins remain protected; evicted in-memory state is lost. See observer placement, disabling and errors.