b_annotations 0.0.2 b_annotations: ^0.0.2 copied to clipboard
Annotations for bloc_generator.
b(loc)_annotations #
Annotations for b_generator
Requirement #
- bloc
- b_annotations
- b_generator
- build_runner
Usage #
- Import bloc
- Add the following to the beginning of your bloc file
part '<filename>.bloc.dart'
part '<filename>.events.dart'
- Annotate the class with
@bloc
- Extend
GeneratedBloc
- implement
initialState
- Add methods for handling events and annotate them with
@event
Example #
When you create my_bloc.dart
import 'package:bloc_annotations/bloc.dart';
import 'package:bloc_generator_example/feed_state.dart';
import 'package:bloc/bloc.dart';
part 'my_bloc.bloc.dart';
part 'my_bloc.events.dart';
@bloc
class MyBloc extends GeneratedBloc {
@override
// Implement this
MyBlocState get initialState => throw UnimplementedError();
@event
Stream<MyBlocState> exampleEvent(final String test) async* {}
}
It will generate the following two files:
my_bloc.bloc.dart
abstract class GeneratedBloc extends Bloc<MyBlocEvent, MyBlocState> {
MyBlocState get initialState;
@override
Stream<MyBlocState> mapEventToState(final MyBlocEvent event) async* {
if (event is ExampleEvent) yield* exampleEvent();
}
Stream<MyBlocState> exampleEvent();
}
my_bloc.events.dart
class MyBlocEvent {}
class ExampleEvent extends MyBlocEvent {
final String test;
ExampleEvent(this.test);
@override
String toString() {
return '$runtimeType { test: $test }';
}
}