flutter_bloc_stream

Helper package for using bloc_stream with Flutter. Includes BlocStreamProvider and BlocStreamBuilder.



Documentation

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';

// State
// We use the Equatable package so we can easily implement the == operator for
// our state.
abstract class EventsState extends Equatable {
  @override
  List<Object> get props => [];
}

class NoEvents extends EventsState {}

class EventsLoaded extends EventsState {
  EventsLoaded(events);

  final List<Event> events;

  @override
  List<Object> get props => events;
}

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

  final EventRepository repository;

  @override
  int get initialValue => NoEvents();

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

Usage with Flutter

Use this package in combination with https://pub.dev/packages/provider and https://api.flutter.dev/flutter/widgets/StreamBuilder-class.html.

You could also use a https://pub.dev/documentation/provider/latest/provider/StreamProvider-class.html from the Provider package.