stream_disposable 0.1.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 87

Build Status

Stream Disposable #

Package to help disposing Streams and Sinks.

Simple Usage #

Instantiate a disposable with

 var disposable = StreamDisposable()

Add StreamSubscriptions or Sinks to it by calling add

   var streamToDispose = Stream.fromIterable([1, 2, 3]);

In the Stateful's Widget dispose method or equivalent, we can call

 disposable.dispose(className: this.runtimeType.toString())

To safely dispose every subscription.

Available Methods #

  • void add Adds a Sink or a StreamSusbcription to the disposable. Throws an Error if another type of object is added or if the disposable has already been disposed.

  • Future<void> didDispose Future that completes when the StreamDisposable is disposed.

  • bool isDisposed Boolean value to check if the disposable has been disposed.

  • Future<void> dispose Dispose the current Sink and StreamSusbcription that were addded to this instance. Throws an error if a dispose action is in process or if the disposable has been disposed already.


  • Changed debugPrint for Print so that Flutter Web can use it

[0.0.1] - Initial Release.

  • Added StreamDisposable class


import 'dart:async';

import 'package:flutter/material.dart';

import 'package:stream_disposable/stream_disposable.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
      home: MyHomePage(title: 'Flutter Demo Home Page'),

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  _MyHomePageState createState() => _MyHomePageState();

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  var disposable = StreamDisposable();

  void _incrementCounter() {
        .push(MaterialPageRoute(builder: (context) => PageB()));

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      body: Center(
        child: Column(
          children: <Widget>[
              padding: EdgeInsets.symmetric(horizontal: 20),
              child: Text(
                'Click on the button to go to the next page and stop counting:',
                softWrap: true,
              style: Theme.of(context).textTheme.display1,
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),

  void initState() {
    var controller = StreamController<int>();
    var sink = controller.sink;
    var stream =;
      stream.listen((data) {
        setState(() {
          _counter = data;


    Timer.periodic(Duration(milliseconds: 500), (i) {
      try {
      } catch (e) {
        print("Closed sink");

  /// When pushing, we will dispose of the stream
  void deactivate() {
    disposable.dispose(className: this.runtimeType.toString());

class PageB extends StatelessWidget {
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Page B"),
      body: Container(
        color: Colors.amber,

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  stream_disposable: ^0.1.0

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:stream_disposable/stream_disposable.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jan 25, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4
  • Flutter: 1.12.13+hotfix.5


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies