Skip to content
pubscout

dispose_scope

2.1.0, released 3 years ago by odrowaz.dev

Utility to group and automatically dispose/cancel/close resources in Dart.

$ flutter pub add dispose_scope
Source repository

What it does

Provides a DisposeScope that tracks objects with dispose, close, or cancel methods and invokes them when the scope is disposed. Includes extension methods for common Dart types like StreamSubscription, Timer, and Process.

AI-written summary of version 2.1.0, based on the package's README and metadata. Report an inaccuracy

Use it when

  • You need to clean up multiple resources (streams, timers, processes) together.
  • You want a simple, dependency‑free way to ensure resources are released when a widget or component is disposed.

Skip it when

  • You are using a framework that already provides its own disposal management (e.g., Flutter's State.dispose).
  • You need compile‑time code generation for disposal.

Strengths

  • Zero external dependencies besides meta.
  • Works with pure Dart, no Flutter required.
  • Provides safety checks and a run‑once dispose guard.

Weaknesses

  • Limited to objects with a dispose/close/cancel method; no automatic detection for custom resources.
  • No code‑gen support; manual registration required.

Setup

Kind
Pure Dart
Effort
Low
Code generation
No
Direct dependencies
1
import 'package:dispose_scope/dispose_scope.dart';

final scope = DisposeScope();

Stream.empty().listen((_) {}).disposedBy(scope);
Timer(Duration.zero, () {}).disposedBy(scope);

scope.dispose();

Alternatives

Packages that do the same job and could replace dispose_scope.

  • disposebag

    Utility to collect and dispose StreamSubscriptions and Sinks in Dart/Flutter.

Works with

Packages commonly used together with dispose_scope: ones it builds on, ones built on it, and its own family.