disposable_utils 1.2.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 57

disposable #

Provides a one abstract mechanism for releasing resources and utils for Dart and Flutter #

Did you notice there is no one defined method to release object resources in Dart and Flutter? StreamController.close(), StreamSubscription.cancel() and many dispose() methods in different Flutter object does the same - they free held information. But they doesn't have any one defined interface for it. Disposable does it. DisposableCollector is a composite of many disposables, which can be disposed as one. It also provides extension methods to make life easier for streams.

Do you recognize it?

class SomeDisposable {
    /// ...
    
    final _subscription = someStream.listen(
      (value) => doSomethingWith(value)
    );
    
    /// ...
    
    void dispose() {
      _subscription.cancel();
    }
}

With DisposableCollector it will looks like :

class SomeDisposable extends DisposableCollector {
  /// ...
  someStream
    .listen((value) => doSomethingWith(value))
    .addTo(this);
  
  /// ...
}

And you can make Disposable from any of your objects with factory

final disposable = Disposable.create(yourObject, () => yourObject.close());
// or
final disposable = yourObject.toDisposable(() => yourObjectClose());

[1.2.0] #

Added equality comparison and hashcode function to disposable. Added toDisposable(void Function(T)) to convert from any object toDisposable of streams and stream controllers removed to asDisposable() Changed Disposable factory function to Disposable.create()

[1.0.0] #

First release. Added Disposable, DisposableCollector and extension methods

example/example.dart

import 'dart:async';
import 'package:disposable_utils/disposable.dart';

void main() {
  final service = new DataService();
  final bloc = new Bloc(service);

  bloc.stream.listen(
    (v) => print(v),
    onDone: () => print("Complete")
  );

  bloc.dispose();

  // You will see printed `Complete`, because bloc was disposed
  // before it receives the value from the data service.
}

/// Just imitation of BLoC as a store of streams
class Bloc extends DisposableCollector {
  Stream<int> get stream => _streamController.stream;
  final StreamController<int> _streamController = new StreamController();

  Bloc(DataService service) {
    _streamController.addTo(this);

    /// Of course instead of construction below you can write something like
    /// ```
    /// final v = await service.getValue();
    /// _streamController.add(v);
    /// ````
    /// or
    /// ```
    /// _streamController.addStream(service.getValue().asStream());
    /// ```
    /// But in that cases you will see different exceptions if you dispose your
    /// streamController before you get the value from your future/stream.
    /// But when you using addTo(Disposable) extension method you will unsubscribe
    /// from the stream when your [StreamController] has been disposed and
    /// there are no exceptions will throw

    service.getValue().asStream()
      .listen((v) => _streamController.add(v))
      .addTo(this);
  }
}

/// Just some data service imitation
class DataService {
  Future<int> getValue() async {
    Future.delayed(Duration(seconds: 1));
    return 3;
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  disposable_utils: ^1.2.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:disposable_utils/disposable.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
15
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]
57
Learn more about scoring.

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

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Health suggestions

Format lib/disposable.dart.

Run flutter format to format lib/disposable.dart.

Format lib/src/disposable.dart.

Run flutter format to format lib/src/disposable.dart.

Format lib/src/disposable_collector.dart.

Run flutter format to format lib/src/disposable_collector.dart.

Format lib/src/disposable_extensions.dart.

Run flutter format to format lib/src/disposable_extensions.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
flutter 0.0.0
mockito ^4.1.1 4.1.1
Transitive dependencies
async 2.4.1
boolean_selector 2.0.0
charcode 1.1.3
collection 1.14.11 1.14.12
matcher 0.12.6
meta 1.1.8
path 1.6.4
sky_engine 0.0.99
source_span 1.7.0
stack_trace 1.9.3
stream_channel 2.0.0
string_scanner 1.0.5
term_glyph 1.1.0
test_api 0.2.15
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test