ezbloc 5.1.3 copy "ezbloc: ^5.1.3" to clipboard
ezbloc: ^5.1.3 copied to clipboard

Make bloc easy again! A simple and low boilerplate implementation of the bloc state management pattern. Includes extras to work with blocs like testing, monitoring and persistence.

A State management library for Dart and Flutter using BLoCs (Business Logic Component)

Checkout the flutter version too: ezbloc_flutter

Why? #

This library provides a simpler alternative for existing libraries which:

  • Require a lot of boilerplate code
  • Expose complexity of underlying reactive streams

Pros 👍 #

  • Easy to use and works out of the box
  • Complete abstraction over Streams and Subscriptions
  • No more states or events boilerplate bloat (just work with functions)

Example #

Easy Bloc hides all streams/yields, exposing only simple functions setState(), setBusy() and setError():

import 'package:ezbloc/ezbloc.dart';

class CounterBloc extends Bloc {
  void increment() => setState(value + 1);
  void decrement() => setState(value - 1);
}

void main() {
  final bloc = CounterBloc();
  
  bloc.increment();
}
copied to clipboard

A bit more complex bloc:

AutoPersistedBloc can be used to save value on app exit, and recover again on app start. Keeps State in sync, shows error messages and loading indicator.

import 'package:ezbloc/ezbloc.dart';

class CounterBloc extends AutoPersistedBloc<CounterBloc, int> {
  void increment() async {
    if (value >= 10) {
      setError(StateError('Counter cannot go beyond 10'));
    }
    
    setBusy();
    await makeNetworkCall();
    
    setState(value + 1);
  }

  void decrement() => setState(value - 1);
}

void main() async {
  final bloc = CounterBloc();
  
  await bloc.increment();
}
copied to clipboard

Bloc Monitor #

You can monitor your blocs and create side effects simply by using a bloc monitor.

 class BroadcastPrinter extends BlocMonitor {
   @override
   void onBroadcast(String blocName, state, {String event}) {
     print('[$blocName] broadcast: $state ($event)');
   }
 }
copied to clipboard

For more details: bloc monitor

Test #

testBloc<CounterBloc, int>(
    'counter should work',
    bloc: () async => CounterBloc(0),
    expectBefore: (bloc) async => expect(bloc.isBusy, false),
    expectAfter: (bloc) async => expect(bloc.hasError, false),
    timeout: Duration(seconds: 1),
    expectedStates: emitsInOrder([0, 1, 2, 1]),
    job: (bloc) async {
      bloc.increment();
      bloc.increment();
      bloc.decrement();
    },
  );
copied to clipboard

Contribution ❤ #

Issues and pull requests are welcome

Please file feature requests and bugs at the issue tracker.

3
likes
140
points
174
downloads

Publisher

verified publishermuha.dev

Weekly Downloads

2024.09.26 - 2025.04.10

Make bloc easy again! A simple and low boilerplate implementation of the bloc state management pattern. Includes extras to work with blocs like testing, monitoring and persistence.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

hive, meta, path, pedantic, quick_log, rxdart, stack_trace, test

More

Packages that depend on ezbloc