opinionated_bloc_generator 0.2.2

  • Readme
  • Changelog
  • Installing
  • new51

opinionated_bloc_generator #

This generator aims to reduce Bloc Library boilerplate in an opinionated way.

In blocs with many events the mapEventToState method tends to be big once you have to handle each event and calls an appropriated method to handle it.

Opinionated Bloc Generator is the way we reduce this kind of bloc boilerplate in ours projects. Maybe it will be useful for you too.

What opinions we have? #

You should use sealed classes to declare bloc states and events. For this we rely on freezed library.

You should handle each event in a separated method inside your bloc.

Hot it works? #

You need to do four simple steps to use the Opinionated Bloc Generator:

1 – Add part of .opinionated.dart after yours imports statements.

2 – Annotate your bloc class with @opinionatedBloc.

3 – Add the generated mixin to your bloc. It will have your bloc name followed by Opinionated.

4 – Implement the required methods in our bloc.

Example #

import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:opinionated_bloc_annotations/opinionated_bloc_annotations.dart';

part 'calculator_bloc.freezed.dart';
part 'calculator_bloc.opinionated.dart';

@freezed
abstract class CalculatorState with _$CalculatorState {
  const factory CalculatorState.done(int result) = Done;
}

@freezed
abstract class CalculatorEvent with _$CalculatorEvent {
  const factory CalculatorEvent.add(int value) = Add;
  const factory CalculatorEvent.subtract(int value) = Subtract;
}

@opinionatedBloc
class CalculatorBloc extends Bloc<CalculatorEvent, CalculatorState>
    with CalculatorBlocOpinionated {
  @override
  CalculatorState get initialState => CalculatorState.done(0);
  var result = 0;

  @override
  Stream<CalculatorState> onSubtract(int value) async* {
    throw UnimplementedError();
  }

  @override
  Stream<CalculatorState> onMultiply(int value) async* {
    throw UnimplementedError();
  }

  @override
  Stream<CalculatorState> onAdd(int value) {
    throw UnimplementedError();
  }
}

when you run pub run build_runner watch --delete-conflicting-outputs a file called calculator_bloc.opinionated.dart will be generated with the follow content:

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'calculator_bloc.dart';

// **************************************************************************
// Generator: OpinionatedBuilder
// **************************************************************************

abstract class CalculatorBlocMapping {
  Stream<CalculatorState> onAdd(int value);
  Stream<CalculatorState> onSubtract(int value);
}

mixin CalculatorBlocOpinionated on Bloc<CalculatorEvent, CalculatorState>
    implements CalculatorBlocMapping {
  @override
  Stream<CalculatorState> mapEventToState(
    CalculatorEvent event,
  ) async* {
    yield* event.when(add: (int value) async* {
      yield* onAdd(value);
    }, subtract: (int value) async* {
      yield* onSubtract(value);
    });
  }
}

Instaling #

To use opinionated_bloc_generator, you will need ad the follow packages to your pubspec.yaml:

dependencies:
  opinionated_bloc_annotation:

dev_dependencies:
  build_runner:
  opinionated_bloc_generator:

Using async* in your method declaration #

To be able to yield states in each event handle you must to declare your method with async*.

Don't #

@override
Stream<CalculatorState> onMultiply(int value) 

Do #

@override
Stream<CalculatorState> onMultiply(int value) async *

0.0.1 - Initial release of generator package. #

0.2.0 - Add example to project #

0.2.1 - Add README.md to example #

0.2.2 - Bump freezed_annotation to ^0.11.0 #

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  opinionated_bloc_generator: ^0.2.2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:opinionated_bloc_generator/opinionated_bloc_generator.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
15
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
70
Overall:
Weighted score of the above. [more]
51
Learn more about scoring.

We analyzed this package on Jul 10, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14

Analysis suggestions

Package not compatible with SDK flutter

Because it is not compatible with any of the supported runtimes: flutter-native, flutter-web

Package not compatible with runtime flutter-native on android

Because:

  • package:opinionated_bloc_generator/opinionated_bloc_generator.dart that imports:
  • package:source_gen/source_gen.dart that imports:
  • package:source_gen/src/type_checker.dart that imports:
  • dart:mirrors

Package not compatible with runtime flutter-native on ios

Because:

  • package:opinionated_bloc_generator/opinionated_bloc_generator.dart that imports:
  • package:source_gen/source_gen.dart that imports:
  • package:source_gen/src/type_checker.dart that imports:
  • dart:mirrors

Package not compatible with runtime flutter-native on linux

Because:

  • package:opinionated_bloc_generator/opinionated_bloc_generator.dart that imports:
  • package:source_gen/source_gen.dart that imports:
  • package:source_gen/src/type_checker.dart that imports:
  • dart:mirrors

Package not compatible with runtime flutter-native on macos

Because:

  • package:opinionated_bloc_generator/opinionated_bloc_generator.dart that imports:
  • package:source_gen/source_gen.dart that imports:
  • package:source_gen/src/type_checker.dart that imports:
  • dart:mirrors

Package not compatible with runtime flutter-native on windows

Because:

  • package:opinionated_bloc_generator/opinionated_bloc_generator.dart that imports:
  • package:source_gen/source_gen.dart that imports:
  • package:source_gen/src/type_checker.dart that imports:
  • dart:mirrors

Package not compatible with runtime flutter-web on web

Because:

  • package:opinionated_bloc_generator/opinionated_bloc_generator.dart that imports:
  • package:source_gen/source_gen.dart that imports:
  • package:source_gen/src/utils.dart that imports:
  • package:build/build.dart that imports:
  • package:build/src/generate/run_post_process_builder.dart that imports:
  • package:build/src/builder/post_process_builder.dart that imports:
  • package:build/src/builder/builder.dart that imports:
  • package:build/src/builder/build_step.dart that imports:
  • package:build/src/asset/reader.dart that imports:
  • package:glob/glob.dart that imports:
  • package:glob/src/list_tree.dart that imports:
  • package:glob/src/io.dart that imports:
  • package:glob/src/io_export.dart that imports:
  • dart:io

Package not compatible with runtime js

Because:

  • package:opinionated_bloc_generator/opinionated_bloc_generator.dart that imports:
  • package:source_gen/source_gen.dart that imports:
  • package:source_gen/src/utils.dart that imports:
  • package:build/build.dart that imports:
  • package:build/src/generate/run_post_process_builder.dart that imports:
  • package:build/src/builder/post_process_builder.dart that imports:
  • package:build/src/builder/builder.dart that imports:
  • package:build/src/builder/build_step.dart that imports:
  • package:build/src/asset/reader.dart that imports:
  • package:glob/glob.dart that imports:
  • package:glob/src/list_tree.dart that imports:
  • package:glob/src/io.dart that imports:
  • package:glob/src/io_export.dart that imports:
  • dart:io

Package not compatible with runtime native-aot

Because:

  • package:opinionated_bloc_generator/opinionated_bloc_generator.dart that imports:
  • package:source_gen/source_gen.dart that imports:
  • package:source_gen/src/type_checker.dart that imports:
  • dart:mirrors

Maintenance issues and suggestions

Support latest dependencies. (-20 points)

The version constraint in pubspec.yaml does not support the latest published versions for 2 dependencies (_fe_analyzer_shared, bloc).

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and opinionated_bloc_generator.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
_fe_analyzer_shared ^4.0.0 4.0.0 5.0.0
analyzer ^0.39.10 0.39.10 0.39.12
bloc ^4.0.0 4.0.0 5.0.1
build ^1.3.0 1.3.0
freezed_annotation ^0.11.0 0.11.0
opinionated_bloc_annotations ^0.0.1 0.0.1
source_gen ^0.9.5 0.9.5 0.9.6
Transitive dependencies
args 1.6.0
async 2.4.2
charcode 1.1.3
collection 1.14.13 1.15.0-nullsafety
convert 2.1.1
crypto 2.1.5
csslib 0.16.1
dart_style 1.3.6
glob 1.2.0
html 0.14.0+3
js 0.6.2
json_annotation 3.0.1
logging 0.11.4
meta 1.2.2 1.3.0-nullsafety
node_interop 1.1.1
node_io 1.1.1
package_config 1.9.3
path 1.7.0
pedantic 1.9.2
pub_semver 1.4.4
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.2.0 1.3.0-nullsafety
watcher 0.9.7+15
yaml 2.2.1
Dev dependencies
build_config ^0.4.2
build_runner ^1.10.0
freezed ^0.10.9
source_gen_test ^0.1.1
test ^1.14.7