LeakDetector class

Tracks bloc lifecycle events for leak detection in debug mode.

LeakDetector helps identify:

  • Unreleased bloc leases (widgets that forget to dispose)
  • Active blocs at app shutdown
  • Orphaned subscriptions

Usage

Enable leak detection in your app's main():

void main() {
  LeakDetector.enable();
  runApp(MyApp());
}

Check for leaks at shutdown or during testing:

// In tests
tearDown(() {
  LeakDetector.checkForLeaks();
  BlocScope.reset();
});

Get detailed leak report:

if (LeakDetector.hasLeaks) {
  print(LeakDetector.getLeakReport());
}

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 Properties

hasLeaks bool
Check if there are any detected leaks.
no setter
isEnabled bool
Whether leak detection is enabled.
no setter
unclosedBlocCount int
Get the number of unclosed blocs.
no setter
unreleasedLeaseCount int
Get the number of unreleased leases.
no setter

Static Methods

checkForLeaks() bool
Check for leaks and print warnings.
disable() → void
Disable leak detection and clear all tracked data.
enable() → void
Enable leak detection.
getLeakReport() String
Generate a detailed leak report.
reset() → void
Clear all tracked data.
trackBlocClose(BlocId id) → void
Track when a bloc is closed.
trackBlocCreation(BlocId id) → void
Track when a bloc is created.
trackLeaseAcquire(BlocId id, {String? context}) → void
Track when a lease is acquired.
trackLeaseRelease(BlocId id) → void
Track when a lease is released.