bloc_code_generator 0.1.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 56

Bloc Code Generator #

A Dart package that generates code for the bloc library.

GenerateBloc Annotation #

GenerateBloc is a dart annotation that requires a State as parameter, the state can be a primitive or an object. It handles the bloc's generation.

How it works #

By adding the annotation to your Bloc class like this:

part 'my_bloc.g.dart';

class MyBloc extends _$Bloc {
  MyState get initialState => MyState();

  Stream<MyState> _mapDoSomethingToState(int someValue, SomeModel someModel) async* {
    yield MyState.doSomething(someValue, someModel);

And running

flutter packages pub run build_runner build --delete-conflicting-outputs

The generator will create the my_bloc.g.dart file for you:

part of 'main_bloc.dart';

enum _$EventType { doSomething }

class _$Event {
  final _$EventType type;
  final List<dynamic> payload;

  _$Event({this.type, this.payload});

abstract class _$Bloc extends Bloc<_$Event, MyState> {
  Stream<MyState> mapEventToState(_$Event event) async* {
    switch (event.type) {
      case _$EventType.doSomething:
        yield* _mapDoSomethingToState(event.payload[0], event.payload[1]);

  void dispatchDoSomethingEvent(int someValue, SomeModel someModel) {
      type: _$EventType.increment,
      payload: [someValue, someModel],

  Stream<MyState> _mapDoSomethingToState(int someValue, SomeModel someModel);

It gets all _map<NAME>ToState methods, creates the dispatchers and maps the right events to them.

By using the generator you don't need to handle the events, only the mapToState methods.

Examples #

  • Counter - an example of how to create a counter bloc

Maintainers #

0.1.1 #

Update dependencies.

0.1.0 #

Improve package Maintenance.

  • Move files to the right paths
  • Update dependencies
  • Update package description

0.0.2 #

Fix analyzer import.

  • Changes the analyzer dependency

0.0.1 #

Initial Version of the library.

  • Includes the ability to generate bloc code from a @GenerateBloc annotation


Bloc Generator Sample #

A Flutter project that shows how Bloc Code Generator works.

Getting Started #

Just run this project as any other flutter project.

Use this package as a library

1. Depend on it

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

  bloc_code_generator: ^0.1.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:bloc_code_generator/annotations.dart';
import 'package:bloc_code_generator/builder.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 Jan 19, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4

Health issues and suggestions

Document public APIs. (-1 points)

6 out of 6 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

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


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
analyzer >=0.32.4 <=0.38.2 0.38.2 0.39.4
build ^1.0.0 1.2.2
source_gen ^0.9.0 0.9.4+6 0.9.4+7
Transitive dependencies
args 1.5.2
async 2.4.0
charcode 1.1.2
collection 1.14.12
convert 2.1.1
crypto 2.1.4
csslib 0.16.1
dart_style 1.2.10 1.3.3
front_end 0.1.24 0.1.29
glob 1.2.0
html 0.14.0+3
js 0.6.1+1
kernel 0.3.24 0.3.29
logging 0.11.4
meta 1.1.8
node_interop 1.0.3
node_io 1.0.1+2
package_config 1.1.0
path 1.6.4
pedantic 1.9.0
pub_semver 1.4.2
source_span 1.6.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+13
yaml 2.2.0