b_annotations 0.0.1

Annotations for bloc_generator.

b(loc)_annotations #

Annotations for bloc_generator

Requirement #

  • bloc
  • bloc_annotations
  • bloc_generator
  • build_runner

Usage #

  1. Import bloc
  2. Add the following to the beginning of your bloc file part '<filename>.bloc.dart' part '<filename>.events.dart'
  3. Annotate the class with @bloc
  4. Extend GeneratedBloc
  5. implement initialState
  6. Add methods for handling events and annotate them with @event

Example #

When you create my_bloc.dart

import 'package:bloc_annotations/b_annotations.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 }';
  }
}
1
likes
20
pub points
29%
popularity

Annotations for bloc_generator.

Uploader

robert.moeller@booza.app

License

MIT (LICENSE)

More

Packages that depend on b_annotations