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
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]
|
2
|
Health:
Code health derived from static analysis.
[more]
|
97
|
Maintenance:
Reflects how tidy and up-to-date the package is.
[more]
|
40
|
Overall:
Weighted score of the above.
[more]
|
38
|
We analyzed this package on Dec 5, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
- Dart: 2.6.1
- pana: 0.12.21
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' 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/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
(Rundartfmt
to formatlib/builder.dart
.)lib/built_bloc_generator.dart
(Rundartfmt
to formatlib/built_bloc_generator.dart
.)lib/src/annotations.dart
(Rundartfmt
to formatlib/src/annotations.dart
.)lib/src/generators/bloc.dart
(Rundartfmt
to formatlib/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. (-20 points)
The version constraint in pubspec.yaml
does not support the latest published versions for 2 dependencies (analyzer
, build_config
).
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.39.1 |
build | >=0.12.6 <2.0.0 | 1.1.0 | 1.2.2 |
build_config | >=0.2.6 <0.4.0 | 0.3.2 | 0.4.1+1 |
built_bloc | ^0.3.5 | 0.3.5 | |
code_builder | ^3.1.3 | 3.2.1 | |
dart_style | ^1.2.2 | 1.2.3 | 1.3.3 |
path | ^1.3.2 | 1.6.4 | |
source_gen | ^0.9.0 | 0.9.4+1 | 0.9.4+6 |
Transitive dependencies | |||
args | 1.5.2 | ||
async | 2.4.0 | ||
built_collection | 4.3.0 | ||
built_value | 7.0.0 | ||
charcode | 1.1.2 | ||
checked_yaml | 1.0.2 | ||
convert | 2.1.1 | ||
crypto | 2.1.3 | ||
csslib | 0.16.1 | ||
fixnum | 0.10.11 | ||
front_end | 0.1.9+1 | 0.1.29 | |
glob | 1.2.0 | ||
html | 0.14.0+3 | ||
js | 0.6.1+1 | ||
json_annotation | 2.4.0 | 3.0.0 | |
kernel | 0.3.9+1 | 0.3.29 | |
matcher | 0.12.6 | ||
meta | 1.1.8 | ||
node_interop | 1.0.3 | ||
node_io | 1.0.1+2 | ||
package_config | 1.1.0 | ||
pedantic | 1.9.0 | ||
plugin | 0.2.0+3 | ||
pub_semver | 1.4.2 | ||
pubspec_parse | 0.1.5 | ||
quiver | 2.1.2+1 | ||
rxdart | 0.22.6 | 0.23.0-dev.2 | |
source_span | 1.5.5 | ||
stack_trace | 1.9.3 | ||
string_scanner | 1.0.5 | ||
term_glyph | 1.1.0 | ||
typed_data | 1.1.6 | ||
watcher | 0.9.7+13 | ||
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.12 | |
logging | ^0.11.3+1 | 0.11.3+2 | |
test | ^1.3.3 | ||
yaml | ^2.1.13 | 2.2.0 |