built_bloc_generator 0.4.1

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


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

part 'example.g.dart';

class ExampleBloc extends Bloc with _ExampleBloc {

  ExampleBloc() {
    _add.map((v) => _count.value + v).listen(_count.add);

  final BehaviorSubject<int> _count = BehaviorSubject<int>.seeded(0);

  final _add = PublishSubject<int>();

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

  void _onReset() {

Use this package as a library

1. Depend on it

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

  built_bloc_generator: ^0.4.1

2. Install it

You can install packages from the command line:

with pub:

$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:built_bloc_generator/built_bloc_generator.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

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

  • Dart: 2.7.1
  • pana: 0.13.6

Health suggestions

Fix lib/src/generators/bind.dart. (-1 points)

Analysis of lib/src/generators/bind.dart reported 2 hints:

line 2 col 8: Unused import: 'package:analyzer/dart/element/type.dart'.

line 37 col 45: The class 'Stream' wasn't exported from 'dart:core' until version 2.1, but this code is required to be able to run on earlier versions.

Fix lib/src/generators/sink.dart. (-0.50 points)

Analysis of lib/src/generators/sink.dart reported 1 hint:

line 2 col 8: Unused import: 'package:analyzer/dart/element/type.dart'.

Fix lib/src/generators/stream.dart. (-0.50 points)

Analysis of lib/src/generators/stream.dart reported 1 hint:

line 2 col 8: Unused import: 'package:analyzer/dart/element/type.dart'.

Fix additional 5 files with analysis or formatting issues. (-0.50 points)

Additional issues in the following files:

  • lib/src/helpers.dart (1 hint)
  • lib/builder.dart (Run dartfmt to format lib/builder.dart.)
  • lib/built_bloc_generator.dart (Run dartfmt to format lib/built_bloc_generator.dart.)
  • lib/src/annotations.dart (Run dartfmt to format lib/src/annotations.dart.)
  • lib/src/generators/bloc.dart (Run dartfmt to format lib/src/generators/bloc.dart.)

Maintenance issues and suggestions

Provide a file named CHANGELOG.md. (-20 points)

Changelog entries help developers follow the progress of your package. See the example generated by stagehand.

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (analyzer).

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
analyzer >=0.34.0 <0.39.0 0.38.5 0.39.6
build ^1.0.0 1.2.2
build_config >=0.3.1 <0.5.0 0.4.2
built_bloc ^0.4.1 0.4.1
code_builder ^3.1.3 3.2.1
dart_style ^1.2.2 1.3.3 1.3.4
meta ^1.1.6 1.1.8
path ^1.3.2 1.6.4
source_gen ^0.9.0 0.9.4+6 0.9.5
Transitive dependencies
args 1.6.0
async 2.4.1
built_collection 4.3.2
built_value 7.0.9
charcode 1.1.3
checked_yaml 1.0.2
collection 1.14.12
convert 2.1.1
crypto 2.1.4
csslib 0.16.1
fixnum 0.10.11
front_end 0.1.27 0.1.29
glob 1.2.0
html 0.14.0+3
js 0.6.1+1
json_annotation 3.0.1
kernel 0.3.27 0.3.29
matcher 0.12.6
node_interop 1.0.3
node_io 1.0.1+2
package_config 1.9.3
pedantic 1.9.0
pub_semver 1.4.4
pubspec_parse 0.1.5
quiver 2.1.3
rxdart 0.23.1 0.24.0-dev.1
source_span 1.7.0
stack_trace 1.9.3
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+14
Dev dependencies
build_runner ^1.0.0
build_test ^0.10.3
logging ^0.11.3+1 0.11.4
test ^1.0.0
yaml ^2.1.13 2.2.0