bloc_code_generator 0.0.2

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

@GenerateBloc(MyState)
class MyBloc extends _$Bloc {
 @override
  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]);
        break;
    }
  }

  void dispatchDoSomethingEvent(int someValue, SomeModel someModel) {
    dispatch(_$Event(
      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.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

Use this package as a library

1. Depend on it

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


dependencies:
  bloc_code_generator: ^0.0.2

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';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
30
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
57
Overall:
Weighted score of the above. [more]
56
Learn more about scoring.

We analyzed this package on Aug 18, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: other

Platform components identified in package: build, io, mirrors.

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.

Fix lib/src/bloc_generator.dart. (-0.50 points)

Analysis of lib/src/bloc_generator.dart reported 1 hint:

line 56 col 9: Use isNotEmpty instead of length

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).

The package description is too short. (-13 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.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and bloc_code_generator.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
analyzer >=0.32.4 <0.36.0 0.35.4 0.38.0
build ^1.0.0 1.1.6
source_gen ^0.9.0 0.9.4+3
Transitive dependencies
args 1.5.2
async 2.3.0
charcode 1.1.2
collection 1.14.12
convert 2.1.1
crypto 2.1.1+1
dart_style 1.2.4 1.2.9
front_end 0.1.14 0.1.22
glob 1.1.7
kernel 0.3.14 0.3.22
logging 0.11.3+2
meta 1.1.7
package_config 1.1.0
path 1.6.4
pedantic 1.8.0+1
pub_semver 1.4.2
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+12
yaml 2.1.16