bloc_stream 3.0.0+1

  • Readme
  • Changelog
  • Example
  • Installing
  • 64

A simple package that helps you to implement the BLoC pattern in your applications.

The BlocStream class is just a Stream with a couple extra methods added to reduce boilerplate. You can subclass it and add your own methods, to implement your business logic.

Internally it uses a BehaviourSubject from the rxdart package to provide data to the stream. The add and addError methods are exposed so you can push state to the stream from your methods.

Here is an example BLoC that provides a list of events:

import 'package:bloc_stream/bloc_stream.dart';
import 'package:equatable/equatable.dart';
import 'package:example/event_repository.dart';

// Bloc
class EventsBloc extends BlocStream<List<Event>> {
  EventBloc(this.repository);

  final EventRepository repository;

  @override
  int get initialValue => null;

  Future<void> fetch() async {
    try {
      final events = await repository.list();
      add(events);
    } catch (err) {
      addError(err);
    }
  }
}

Usage with Flutter #

See flutter_bloc_stream.

You can also use this package in combination with Provider and StreamBuilder.

You could also use a StreamProvider from the Provider package.

3.0.0+1 #

  • Update examples

3.0.0 #

  • Switch back to original API

2.1.0 #

  • Remove asObservable

2.0.1 #

  • Add listen method.

2.0.0 #

  • Move output stream to .stream to reduce API surface.

1.1.0 #

  • Mark some methods as @protected.

1.0.1 #

  • initialValue now defaults to null.

1.0.0+3 #

  • Fix links in README

1.0.0+2 #

  • Fix CHANGELOG.md

1.0.0+1 #

  • Update example.

1.0.0 #

  • Initial version

example/example.dart

import 'package:bloc_stream/bloc_stream.dart';

class CounterBloc extends BlocStream<int> {
  @override
  int get initialValue => 0;

  void increment() {
    add(value + 1);
  }

  void decrement() {
    add(value - 1);
  }
}

class MultiplicationBloc extends BlocStream<int> {
  MultiplicationBloc(CounterBloc counter) : _counter = counter {
    // Bloc has a helper method for cleaning up subscriptions without having to
    // write a boilerplate close() override.
    cancelOnClose(counter.distinct().listen((count) {
      add(_multiply(count));
    }));
  }

  final CounterBloc _counter;

  @override
  int get initialValue => _multiply(_counter.value);

  int _multiply(int count) {
    return count * 3;
  }
}

void main() {
  final counter = CounterBloc();
  final multiplier = MultiplicationBloc(counter);

  // Prints:
  // 0
  // MULTIPLY 0
  // 1
  // 2
  // MULTIPLY 3
  // 3
  // MULTIPLY 6
  // 2
  // MULTIPLY 9
  // 1
  // MULTIPLY 6
  // MULTIPLY 3
  counter.distinct().listen(print);
  multiplier.distinct().listen((i) => print('MULTIPLY $i'));

  counter.increment();
  counter.increment();
  counter.increment();
  counter.decrement();
  counter.decrement();
}

Use this package as a library

1. Depend on it

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


dependencies:
  bloc_stream: ^3.0.0+1

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:bloc_stream/bloc_stream.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
28
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]
64
Learn more about scoring.

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

  • Dart: 2.8.1
  • pana: 0.13.8-dev

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.5.0 <3.0.0
meta >=1.0.0 <2.0.0 1.1.8
rxdart >=0.23.0 <1.0.0 0.24.1
Dev dependencies
pedantic ^1.8.0
test ^1.6.0