b_annotations 0.0.2
b_annotations: ^0.0.2 copied to clipboard

Dart native js
Flutter Android iOS web

Annotations for bloc_generator.

b(loc)_annotations #

Annotations for b_generator

Requirement #

  • bloc
  • b_annotations
  • b_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/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 }';
  }
}
1
likes
70
pub points
18%
popularity

Annotations for bloc_generator.

Documentation

API reference

Uploader

robert.moeller@booza.app

License

MIT (LICENSE)

Dependencies

meta

More

Packages that depend on b_annotations