built_bloc_generator 0.4.1

Dart native

Generates the BLoC boilerplate.

built_bloc_generator #

Generate the BLoC pattern boilerplate.

Quickstart #

In order to generate your bloc, your class must extend Bloc, be annotated with @bloc and with a mixin named _<class name>.

Then declare your subject fields and annotate them with @stream or @sink to generate a public getter accordingly.

You can also subscribe to a subject with the Bind annotation.

import 'package:rxdart/rxdart.dart';
import 'package:built_bloc/built_bloc.dart';

part 'example.g.dart';

class ExampleBloc extends Bloc with _ExampleBloc {
  final BehaviorSubject<int> _count = BehaviorSubject<int>.seeded(0);

  final PublishSubject<int> _add = PublishSubject<int>();

  final PublishSubject<void> _reset = PublishSubject<void>();

  void _onAdd(int value) {
    this._count.add(this._count.value + value);

  void _onReset() {

This ExampleBloc will expose your streams and sinks.

final myBloc = ExampleBloc();

myBloc.count.listen((v) {
    print("count: $v");


How to use #

Install #

There are a few separate packages you need to install:



Custom names #

To control how sink and stream getters are generated you can specify names by using @BlocStream("custom") / @BlocSink("custom") instead of @stream/@sink.

Both Sink and Stream #

If you want that a subject to be both exported sa Sink and Stream, you can add two annotations on a unique property.

By default, you sink will be renamed update<name>.

Run the generator #

To run the generator, you must use build_runner cli:

flutter pub pub run build_runner watch