FeatureScope class
Groups multiple feature-level blocs for collective lifecycle management.
When a feature or user flow requires multiple blocs that should share the same lifecycle, create a FeatureScope and register blocs with it. When the feature completes, call end to dispose all managed blocs.
Example:
class CheckoutFlow {
late final FeatureScope scope;
Future<void> start() async {
scope = await FeatureScope.create('checkout');
BlocScope.register<CartBloc>(() => CartBloc(),
lifecycle: BlocLifecycle.feature, scope: scope);
BlocScope.register<PaymentBloc>(() => PaymentBloc(),
lifecycle: BlocLifecycle.feature, scope: scope);
}
Future<void> complete() => scope.end();
}
Reactive Lifecycle with ScopeLifecycleBloc
When ScopeLifecycleBloc is registered, FeatureScope provides reactive lifecycle:
// Register ScopeLifecycleBloc first
BlocScope.register<ScopeLifecycleBloc>(() => ScopeLifecycleBloc(),
lifecycle: BlocLifecycle.permanent);
// Create scope - automatically registers with ScopeLifecycleBloc
final scope = await FeatureScope.create('checkout');
// End triggers cleanup sequence:
// 1. ScopeLifecycleBloc publishes ScopeEndingNotification
// 2. Subscribers register cleanup futures
// 3. CleanupBarrier awaited with timeout
// 4. Blocs disposed
await scope.end();
Without ScopeLifecycleBloc, FeatureScope works but without reactive cleanup.
Constructors
- FeatureScope([String name = 'unnamed'])
- Creates a feature scope with an optional name for debugging.
Properties
- hashCode → int
-
The hash code for this object.
no setteroverride
- isEnded → bool
-
Whether this scope has been ended.
no setter
- isEnding → bool
-
Whether end() has been called (but may not be complete).
no setter
-
managedBlocs
→ Set<
BlocId> -
All bloc IDs managed by this scope.
no setter
- name → String
-
Human-readable name for debugging purposes.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- scopeId → String?
-
The scope ID assigned by ScopeLifecycleBloc, if started with ScopeLifecycleBloc.
no setter
Methods
-
end(
) → Future< EndScopeResult> - End this feature scope and dispose all managed blocs.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
start(
) → Future< void> - Explicitly start the scope and register with ScopeLifecycleBloc.
-
toString(
) → String -
A string representation of this object.
override
-
track(
Type type) → void - Track a bloc type as managed by this scope.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
override
Static Methods
-
create(
String name) → Future< FeatureScope> - Create a FeatureScope and start it.
-
debugCheckLeaks(
) → void - Check for un-ended feature scopes (debug only).
-
resetTracking(
) → void - Clear all tracked scopes.