built_bloc_generator 0.3.5

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

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

  @sink
  @Bind("_onAdd")
  final PublishSubject<int> _add = PublishSubject<int>();

  @sink
  @Bind("_onReset")
  final PublishSubject<void> _reset = PublishSubject<void>();

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

  void _onReset() {
    this._count.add(0);
  }
}

This ExampleBloc will expose your streams and sinks.

final myBloc = ExampleBloc();

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

myBloc.add.add(42);

How to use #

Install #

There are a few separate packages you need to install:

dependencies:
  built_bloc:

dev_dependencies:
  built_bloc_generator: 
  build_runner: 

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

example/example.dart

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

part 'example.g.dart';

@bloc
class ExampleBloc extends Bloc with _ExampleBloc {

  ExampleBloc() {
    _add.map((v) => _count.value + v).listen(_count.add);
    //_reset.mapTo(0).listen(_count.add);
  }

  //@BlocStream("total")
  //@BlocSink("setTotal")
  @sink
  @stream
  final BehaviorSubject<int> _count = BehaviorSubject<int>.seeded(0);

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

  @sink
  @Bind("_onReset")
  final PublishSubject<void> _reset = PublishSubject<void>();

  void _onReset() {
    this._count.add(0);
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  built_bloc_generator: ^0.3.5

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

We analyzed this package on Jul 20, 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

Primary library: package:built_bloc_generator/built_bloc_generator.dart with components: io, build, mirrors.

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' was not exported from 'dart:core' until version 2.1, but this code is required to be able to run on earlier versions.

Fix lib/src/helpers.dart. (-1 points)

Analysis of lib/src/helpers.dart reported 2 hints:

line 37 col 12: Don't explicitly initialize variables to null.

line 46 col 28: 'computeNode' is deprecated and shouldn't be used.

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 additional 5 files with analysis or formatting issues. (-0.50 points)

Additional issues in the following files:

  • lib/src/generators/stream.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

Support latest dependencies. (-50 points)

The version constraint in pubspec.yaml does not support the latest published versions for 5 dependencies (analyzer, build, build_config, dart_style, source_gen).

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

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

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.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
analyzer >=0.32.2 <0.35.0 0.34.3 0.37.0
build >=0.12.6 <2.0.0 1.1.0 1.1.5
build_config >=0.2.6 <0.4.0 0.3.2 0.4.1
built_bloc ^0.3.5 0.3.5
code_builder ^3.1.3 3.2.0
dart_style ^1.2.2 1.2.3 1.2.9
path ^1.3.2 1.6.2
source_gen ^0.9.0 0.9.4+1 0.9.4+3
Transitive dependencies
args 1.5.2
async 2.3.0
built_collection 4.2.2
built_value 6.7.0
charcode 1.1.2
convert 2.1.1
crypto 2.0.6
csslib 0.16.1
fixnum 0.10.9
front_end 0.1.9+1 0.1.20
glob 1.1.7
html 0.14.0+2
json_annotation 2.4.0
kernel 0.3.9+1 0.3.20
matcher 0.12.5
meta 1.1.7
package_config 1.0.5
pedantic 1.8.0+1
plugin 0.2.0+3
pub_semver 1.4.2
pubspec_parse 0.1.4
quiver 2.0.3
rxdart 0.22.0
source_span 1.5.5
stack_trace 1.9.3
string_scanner 1.0.4
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+12
Dev dependencies
build_runner ^1.0.0
build_test ^0.10.0
build_verify ^1.1.0
build_web_compilers ^1.0.0
collection ^1.14.0 1.14.11
logging ^0.11.3+1 0.11.3+2
test ^1.3.3
yaml ^2.1.13 2.1.16

Admin