bloc_code_generator 0.1.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 64

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

example/README.md

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:


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

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

  • Dart: 2.5.1
  • pana: 0.12.21

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.

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

Dependencies

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.0
build ^1.0.0 1.2.0
source_gen ^0.9.0 0.9.4+5
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.3
csslib 0.16.1
dart_style 1.2.10 1.3.2
front_end 0.1.24 0.1.28
glob 1.2.0
html 0.14.0+3
js 0.6.1+1
kernel 0.3.24 0.3.28
logging 0.11.3+2
meta 1.1.7
node_interop 1.0.3
node_io 1.0.1+2
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.2.0