broadcast_stream_subscriber 0.1.0

broadcast_stream_subscriber #

A Dart mixin with a minimal set of methods to add and manage [StreamSubscription]s and notify them of events with a broadcast (multi-stream) [StreamController].

Usage #

import 'package:broadcast_stream_subscriber/broadcast_stream_subscriber.dart';

// Mixin `BoradcastStreamSubscriber` to your class
class ClassName with BroadcastStreamSubscriber<T> {
  ClassName();

  // Dispose of your class
  void dispose() {
    // Cancel any still active `StreamSubscriptions`
    // and close the `StreamController`.
    closeStream();
  }
}

// Create an instance of your class
final ClassName _className = ClassName();

// Add a listener
_className.addListener((T event) {
  // Handle the event...
});

// Notify listeners of an event
_className.notifyListeners(eventData);

// Remove a listener
_className.removeListener();

// Dispose of your class. Alternatively call `ClassName.closeStream()` directly
// if nothing else from the class needs to be cancelled/closed/disposed.
_className.dispose();

Using with Flutter #

[BroadcastStreamSubscriber] can be used within Flutter widgets to listen for events from any class that mixes it in.

To properly subscribe to events and dispose of the notifier when finished, [BroadcastStreamSubscriber]'s methods should be implemented from within a [StatefulWidget], or rather its [State].

  import 'package:flutter/material.dart';
  import 'package:broadcast_stream_subscriber/broadcast_stream_subscriber.dart';

  class ListenableClass with BroadcastStreamSubscriber<String> {
    ListenableClass();

    //

    void dispose() => closeStream();
  }

  class MyWidget extends StatefulWidget {
    @override
    _MyWidgetState createState() => _MyWidgetState();
  }

  class _MyWidgetState extends State<MyWidget> {
    final ListenableClass _listenableClass = ListenableClass();

    StreamSubscription<String> _onEvent;

    String _eventData;

    @override
    void initState() {
      super.initState();
      _listenForEvents();
    }

    @override
    void dispose() {
      _onEvent.cancel();
      _listenableClass.closeStream();
      super.dispose();
    }

    void _listenForEvents() {
      _onEvent = _listenableClass.addListener((String event) {
        setState(() => _eventData = event);
      });
    }

    @override
    Widget build(BuildContext context) {
      return Text(_eventData ?? 'No event data recieved');
    }
  }

If a listenable class is inherited by a nested widget, the [State]'s [didUpdateWidget] method should be utilized to detect and handle any changes to the listener.

And, rather than calling [closeStream] on dispose(), [removeListener] should instead be used. [closeStream] should be called from within the parent.

  class MyWidget extends StatefulWidget {
    const MyWidget(this.listenableClass);

    final ListenableClass listenableClass;

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

  class _MyWidgetState extends State<MyWidget> {
    StreamSubscription<String> _onEvent;

    String _eventData;

    @override
    void initState() {
      super.initState();
      _listenForEvents();
    }

    @override
    void dispose() {
      _onEvent?.cancel();
      widget.listenableClass?.removeListener();
      super.dispose();
    }

    @override
    void didUpdateWidget(MyWidget old) {
      if (widget.listenableClass != old.listenableClass) {
        _onEvent?.cancel();
        _listenForEvents();
      }

      super.didUpdateWidget(old);
    }

    void _listenForEvents() {
      if (widget.listenableClass == null) return;

      _onEvent = widget.listenableClass.addListener((String event) {
        setState(() => _eventData = event);
      });
    }

    @override
    Widget build(BuildContext context) {
      return Text(_eventData ?? 'No event data recieved');
    }
  }

[0.1.0] - July 17, 2019

  • Initial release

Use this package as a library

1. Depend on it

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


dependencies:
  broadcast_stream_subscriber: ^0.1.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or 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:broadcast_stream_subscriber/broadcast_stream_subscriber.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
4
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

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

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:broadcast_stream_subscriber/broadcast_stream_subscriber.dart.

Health suggestions

Format lib/broadcast_stream_subscriber.dart.

Run dartfmt to format lib/broadcast_stream_subscriber.dart.

Maintenance suggestions

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and broadcast_stream_subscriber.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
Dev dependencies
test ^1.6.5