b_generator 0.0.2+1
b_generator: ^0.0.2+1 copied to clipboard

Generators for b_annotations.

b(loc)_generator #

Generators for b_annotations

Disclaimer #

This package is intended for personal use, so use at own risk. It is based on flutter_bloc: ^1.0.0 and might not work for later versions.

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:b_annotations/bloc.dart';
import 'package:b_generator_example/feed_state.dart';
import 'package:bloc/bloc.dart';

part 'my_bloc.bloc.dart';

part 'my_bloc.events.dart';

class MyBloc extends GeneratedBloc {
  // Implement this
  MyBlocState get initialState => throw UnimplementedError();

  Stream<MyBlocState> exampleEvent(final String test) async* {}

It will generate the following two files:


abstract class GeneratedBloc extends Bloc<MyBlocEvent, MyBlocState> {
  MyBlocState get initialState;

  Stream<MyBlocState> mapEventToState(final MyBlocEvent event) async* {
    if (event is ExampleEvent) yield* exampleEvent();

  Stream<MyBlocState> exampleEvent();


class MyBlocEvent {}

class ExampleEvent extends MyBlocEvent {
  final String test;


  String toString() {
    return '$runtimeType { test: $test }';