BlocScope class

Manages bloc registration, resolution, and lifecycle.

BlocScope provides lifecycle-aware management of bloc instances:

  • permanent: Lives for entire app lifetime
  • feature: Lives until FeatureScope ends
  • leased: Lives while widgets hold leases

Registration

// App-level blocs (registered at startup)
BlocScope.register<AuthBloc>(() => AuthBloc(),
    lifecycle: BlocLifecycle.permanent);

// Feature-level blocs
final scope = FeatureScope('checkout');
BlocScope.register<CartBloc>(() => CartBloc(),
    lifecycle: BlocLifecycle.feature, scope: scope);

// Widget-level blocs (can also be registered by widgets)
BlocScope.register<FormBloc>(() => FormBloc(),
    lifecycle: BlocLifecycle.leased);

Resolution

// For permanent/feature blocs
final auth = BlocScope.get<AuthBloc>();

// For leased blocs (required)
final lease = BlocScope.lease<FormBloc>();
// ... use lease.bloc ...
lease.dispose(); // Release when done

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Methods

checkForLeaks() → bool
Check for memory leaks and print a report.
debugDump() → void
Dump all registered blocs and their state (debug only).
diagnostics<T extends JuiceBloc<BlocState>>({Object? scope}) → BlocDiagnostics?
Get diagnostic info for a specific bloc.
enableLeakDetection() → void
Enable leak detection for debugging.
end<T extends JuiceBloc<BlocState>>({Object? scope}) → Future<void>
End a specific bloc instance.
endAll() → Future<void>
Dispose all blocs (app shutdown).
endFeature(FeatureScope scope) → Future<void>
End all blocs associated with a FeatureScope.
get<T extends JuiceBloc<BlocState>>({Object? scope}) → T
Get a bloc instance (creates lazily if needed).
isRegistered<T extends JuiceBloc<BlocState>>({Object? scope}) → bool
Check if a bloc type is registered.
lease<T extends JuiceBloc<BlocState>>({Object? scope}) → BlocLease<T>
Acquire a lease on a bloc.
leaseAsync<T extends JuiceBloc<BlocState>>({Object? scope}) → Future<BlocLease<T>>
Acquire a lease asynchronously, waiting if bloc is closing.
register<T extends JuiceBloc<BlocState>>(T factory(), {BlocLifecycle lifecycle = BlocLifecycle.permanent, Object? scope}) → void
Register a bloc factory with lifecycle behavior.
reset() → Future<void>
Clear all registrations (for testing).
validateRegistration<T extends JuiceBloc<BlocState>>({Object? scope, required BlocLifecycle expectedLifecycle}) → void
Validate that a registration matches expected lifecycle.