stream_disposable 0.1.0

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]);
   disposable.add(streamToDispose.listen(print))

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.

[0.1.0]

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

[0.0.1] - Initial Release.

  • Added StreamDisposable class

example/lib/main.dart

import 'dart:async';

import 'package:flutter/material.dart';

import 'package:stream_disposable/stream_disposable.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

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

  var disposable = StreamDisposable();

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

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

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

    disposable.add(sink);

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

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

class PageB extends StatelessWidget {
  @override
  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:


dependencies:
  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';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
64
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
82
Learn more about scoring.

We analyzed this package on Sep 16, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.0
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.2

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

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.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test