lightweight_bloc 0.0.4

  • Readme
  • Changelog
  • Installing
  • 66

lightweight_bloc #

A lightweight implementation of the Bloc pattern

Purpose #

This package loosely follows the Bloc pattern with minimized boilerplate code as the top priority.

Insipiration #

lightweight_bloc is heavily inspired by flutter_bloc. If you want a highly scalable and featrue-rich bloc package, use it instead.

Usage #

BlocProvider #

Provider a Bloc to all the nodes below it in the widget trees.

    child: AScreen()
    builder: (context) => ABloc()

Then in the child widgets, you can access ABloc like this

final aBloc = BlocProvider.of<ABloc>(context);

Bloc #

Handles business logic and provides States to observers. Every bloc should:

  • Extend the Bloc class and pass in it’s State class.
class ABloc extends Bloc<AState>{
  • Provides initialState, which is the state that this Bloc starts with.
AState get initialState => AState(state: Loading)
  • Define event handling functions, usually starts with “on…”,
  • Use latestState to access the latest State object up until then.
  • Then use update to emits a new State. This State should usually be a clone from the previous State, with different State identifier/data.
void onUserRefreshPage() async {
  if(latestState.stateId == AState.DoneLoading){
    updateState(latestState.copyWith(stateId: AState.Loading);
    // Fetch new data

State #

Data output of a Bloc. Every state should:

  • Have State identifiers.
  • Have a copyWith function to clone the current State with some new data.
class AState {
  static const String Loading = "AStateLoading";
  static const String DoneLoading = "AStateDoneLoading";
  String stateId;
  AState copyWith({String stateId}) => AState(
    stateId: stateId ?? this.stateId

BlocListener #

A flutter widget provides a listener function that is guaranteed to be called only once every time a bloc’s state changes. This should be used to handle one-time effecs in the UI like navigation, showing a dialog, using controllers.

 return BlocListener<ABloc, AState>(
  child: ...
  listener: (context, bloc, state){
    if(state.stateId == AState.DoneLoading){
      showSnackBar("New data loaded");

BlocWidgetBuilder #

A flutter widget provides a builder function to render the widgets with the associated State.

 return BlocListener<ABloc, AState>(
  child: ...
  builder: (context, bloc, state){
    if(state.stateId == AState.DoneLoading){
      return _buildPage(state);
    } else {
      return _buildLoadingIndicator();

Tips and Tricks #

Nullable #

Sometimes with new events, we want to clear some data in the State. So we might do something like latestState.copyWith(data: null). However, copyWith doesn’t understand and thinks we didn’t pass any value to it.

class AState {
  int data;
  // Passing null is useless, copyWith will always get the current data
  AState copyWith({int data}) => AState(
    data: data ??

In these cases, we can use Nullable to differentiate between passing a null value and not passing any value at all.

class AState {
  int data;
  // Passing null is useless, copyWith will always get the current data
  AState copyWith({Nullable<int> data}) => AState(
    data: data != null ? data.value : this.value

Plugin #

Dart Data Class to generate data class with copyWith function.

[0.0.4] #

  • Add MultiBlocProvider

[0.0.3+1] #

  • Fix BlocObserver import

[0.0.3] #

  • [BREAKING] Default to auto init bloc upon construction
  • Public Nullable class

[0.0.2] #

  • Use State instead of Model for better consistency.

[0.0.1] #

  • Add basic functionality

Use this package as a library

1. Depend on it

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

  lightweight_bloc: ^0.0.4

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter pub get

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

3. Import it

Now in your Dart code, you can use:

import 'package:lightweight_bloc/lightweight_bloc.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

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

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Health issues and suggestions

Fix lib/src/bloc_provider.dart. (-5.48 points)

Analysis of lib/src/bloc_provider.dart reported 1 warning, 1 hint:

line 50 col 12: The parameter 'create' is required. .

line 52 col 7: 'builder' is deprecated and shouldn't be used. Will be removed as part of 4.0.0, use create instead.

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 (provider, rxdart).

The package description is too short. (-12 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

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 lightweight_bloc.dart. Packages with multiple examples should provide example/

For more information see the pub package layout conventions.

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
provider ^3.1.0+1 3.2.0 4.0.4
rxdart ^0.18.1 0.18.1 0.23.1
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies