Tutorials
Keep a draft between visits
A complete Dart program for state retention, lease return, reacquisition and final close.
Add ark_di: ^1.3.1, save the program as bin/main.dart and run dart run. Reopening prints Unsent draft. This is in-memory retention, not disk persistence.
Retain state between visits
For example, keep an unsent draft while its feature is temporarily closed. Create the cache at a stable application/session owner, not on every visit. Create each definition once too: its identity and immutable arguments form the key.
This complete program opens a draft, releases access, reopens the same draft, and finally closes the owning application:
import 'package:ark_di/ark_di.dart';
Future<void> main() async {
final DiContainer application = DiContainer.build((final DiBinder binder) {});
final DiScopeCache cache = application.createScopeCache(
policy: const DiScopeCachePolicy(
maxScopes: 2,
idleTimeout: Duration(minutes: 5),
),
);
final DiScopeDefinition<String> drafts = DiScopeDefinition<String>(
configure: (final DiBinder binder, final String documentId) {
binder.bindLazySingleton<_Draft>((final DiResolver resolver) => _Draft());
},
expose: (final String documentId) => <DiDependencyDescriptor>[
const DiDependencyDescriptor(type: _Draft),
],
);
try {
final DiScopeLease first = await cache.acquire(drafts, arguments: 'document-7');
try {
first.resolver.get<_Draft>().text = 'Unsent draft';
} finally {
await first.release();
}
// The feature is idle, not global. Reopen the same definition and arguments.
final DiScopeLease reopened =
await cache.acquire(drafts, arguments: 'document-7');
try {
print(reopened.resolver.get<_Draft>().text); // Unsent draft
print(cache.diagnostics.scopes.single.state); // DiCachedScopeState.inUse
} finally {
await reopened.release();
}
} finally {
// Final close also closes the cache and its retained child containers.
await application.close();
}
}
final class _Draft {
// Empty text is a valid initial draft, not a missing identifier.
String text = '';
}
Each acquire returns its own DiScopeLease. Release every lease, including
on failures. Release revokes that lease's resolver immediately; it does not
close a graph still used by another lease. tryAcquire returns a ready lease
synchronously or null, without creating a graph. Transient factories still
create new objects; caching a scope does not turn a factory into a singleton.
The owner explicitly chooses maxScopes and idleTimeout:
- TTL starts after the last lease and view child leave; reopening restarts the next idle period.
- At capacity, the oldest idle graph is closed before another graph uses its slot.
Active/reserved/retiring slots cannot be silently displaced. If no slot is
available, acquisition fails with
DiScopeCacheCapacityException. trimIdle()closes unused graphs.invalidate/invalidateAllalso revoke active generations; the cache can subsequently create replacements.cache.close()is final. Parentclose()also closes its caches. For logout/account changes close or invalidate the relevant session scopes; TTL alone is not an account-isolation policy.
Do not put BuildContext, widget State, tickers or screen-owned controllers in
retained state. Use an owned view child and close it first. DI cannot revoke
an object already returned to application code or automatically release a
forgotten lease through garbage collection.
See the retained-scope guide for exact ownership, expiry, cleanup errors and asynchronous setup boundaries. The modular cart example also demonstrates modules, synchronous reuse and final close.