disposebag

Author: Petrus Nguyễn Thái Học

Dart CI Codacy Badge Pub Pub codecov Build Status GitHub Style

A package helps to cancel StreamSubscriptions and close Sinks.

Medium article

How to easily cancel streams in Flutter

Usage

A simple usage example:

import 'package:disposebag/disposebag.dart';
import 'dart:async';

main() async {
  final controllers = <StreamController>[];
  final subscriptions = <StreamSubscription>[];

  final bag = DisposeBag([...subscriptions, ...controllers]);

  await Stream.value(3).listen(null).disposedBy(bag);
  await StreamController<int>.broadcast().disposedBy(bag);
  await StreamController<int>.broadcast(sync: true).disposedBy(bag);

  await bag.dispose();
  print("Bag disposed. It's all good");
}

API

1. Add, addAll

Future<bool> DisposeBag.add(StreamSubscription);
Future<bool> DisposeBag.add(Sink);
Future<void> DisposeBag.addAll(Iterable<StreamSubscription>);
Future<void> DisposeBag.addAll(Iterable<Sink>);

// extension methods
Future<bool> StreamSubscription.disposedBy(DisposeBag);
Future<bool> Sink.disposedBy(DisposeBag);
Future<void> Iterable<StreamSubscription>.disposedBy(DisposeBag);
Future<void> Iterable<Sink>.disposedBy(DisposeBag);

2. Delete (removes but does not dispose)

bool delete(StreamSubscription);
bool delete(Sink);

3. Remove (removes and disposes)

Future<bool> remove(StreamSubscription);
Future<bool> remove(Sink);

4. Clear, dispose

Future<void> clear();
Future<void> dispose();

Features and bugs

Please file feature requests and bugs at the issue tracker.

Libraries

disposebag
Helper disposing Streams and closing Sinks