Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aloisdeniel/built_bloc
Generate the BLoC pattern boilerplate.
https://github.com/aloisdeniel/built_bloc
bloc dart flutter generator pattern
Last synced: 11 days ago
JSON representation
Generate the BLoC pattern boilerplate.
- Host: GitHub
- URL: https://github.com/aloisdeniel/built_bloc
- Owner: aloisdeniel
- License: mit
- Created: 2019-01-15T08:21:45.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-10-27T15:05:37.000Z (about 4 years ago)
- Last Synced: 2024-10-11T22:42:58.604Z (27 days ago)
- Topics: bloc, dart, flutter, generator, pattern
- Language: Dart
- Size: 119 KB
- Stars: 51
- Watchers: 3
- Forks: 5
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# built_bloc
Generate the BLoC pattern boilerplate.
## Why ?
After using the [BLoC pattern](https://medium.com/flutter-io/build-reactive-mobile-apps-in-flutter-companion-article-13950959e381) a little, it seems pretty cool for sharing code and separation of concerns, but I quickly found myself to write a **lot of repetitive boilerplate code** : I felt the need for a generator to assist me.
Here is a simple vanilla bloc example which obviously highlights repeated patterns while declaring subjects, streams, sinks and subscriptions.
```dart
class VanillaExampleBloc {Sink get reset => this._reset.sink;
Sink get add => this._add.sink;
Stream get count => this._count.stream;
final PublishSubject _add = PublishSubject();
final PublishSubject _reset = PublishSubject();
final BehaviorSubject _count = BehaviorSubject.seeded(0);
List subscriptions;
List subjects;
VanillaExampleBloc() {
subscriptions = [
this._add.listen(_onAdd),
this._reset.listen(_onReset),
];
subjects = [
this._add,
this._count,
this._reset,
];
}void _onAdd(int value) {
this._count.add(this._count.value + value);
}void _onReset(int value) {
this._count.add(0);
}@mustCallSuper
void dispose() {
this.subjects.forEach((s) => s.close());
this.subscriptions.forEach((s) => s.cancel());
}
}
```With **built_bloc**, you can replace all of that with just a few lines of code :
```dart
@bloc
class ExampleBloc extends Bloc with _ExampleBloc {
@stream
final BehaviorSubject _count = BehaviorSubject(seedValue: 0);@sink
@Bind("_onAdd")
final PublishSubject _add = PublishSubject();@sink
@Bind("_onReset")
final PublishSubject _reset = PublishSubject();void _onAdd(int value) {
this._count.add(this._count.value + value);
}void _onReset() {
this._count.add(0);
}
}
```## How to use ?
See the [built_bloc_generator](https://pub.dartlang.org/packages/built_bloc_generator) package.