Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

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.