common_bloc 0.1.2+1

  • Readme
  • Changelog
  • Example
  • Installing
  • new59

common_bloc #

A collection of common blocs that maybe will be used very often.

Idea of this #

Sometimes we need to create various blocs in the same project to do almost the same thing. This is a collection of blocs to no write so many code.

Powered by #

Blocs #

RestBloc #

This is a bloc to make network requests over a REST API.


To use it, we need to create an instance:

final restBloc = RestBloc('<baseUrl>'); //BaseURL of REST API.

Also, we can pass a list of interceptors:

final restBloc = RestBloc('<baseUrl>', interceptors: []);

Uses the InterceptorContract class of http_interceptor package to listen to request and response.

After that, we can use the following methods:

restBloc.get('path'); // With optional params as a Map<String, String>.'path', body: body); // With body as a String.
restBloc.put('path', body: body); // With body as a String.
restBloc.patch('path', body: body); // With body as a String.

In all of the methods (except 'delete'), we have an optional param named: fromJson

restBloc.get('path', fromJson: fromJson);

This must be a Function to convert the response into an object. Expects to work with json_serilizable and BuiltValue

restBloc.get('path', fromJson: Data.fromJson);

In that, the 'data' object will be of that type.


As a bloc, we have states to manage the request and because is powered by freezed, we can use something like:

BlocBuilder<RestBloc, RestState>(
  bloc: restBloc,
  builder: (context, state) => state.when(
    uninitialized: () => Container(), //Widget for UninitializedState
    loading: () => Container(), //Widget for LoadingState
    error: (error) => Container(), //Widget for ErrorState
    loaded: (data, lastPath, timestamp) => Container() //Widget for LoadedState

RequestBloc #

This bloc will be used when we want to make a task, a request, o something that needs a response/data/result.


This bloc doesn't need params to be constructed.

final requestBloc = RequestBloc();

When we need to perform an action, we call:

requestBloc.perform(action, actionName);

'action' is a function that returns a Future to be resolved and will be the result.

'actionName' is a identifier of what we are doing.

requestBloc.perform(() async => 'Hi!', 'Waving');


This use the same states of RestBloc:

  • uninitialized
  • loading
  • error
  • loaded

In the future #

  • [ ] Tab Bloc (for controlling tab behaviour).
  • [ ] Session Bloc.
  • [ ] Login/Register Bloc.
  • [ ] A suggestion?.

[0.1.2+1] #

  • perform method with loading by default

[0.1.2] #

  • Optional loading for Request bloc

[0.1.1] #

  • Example added
  • Update dependencies

[0.1.0] - Initial release #

  • Package created
  • RestBloc added
  • RequestBloc added


import 'package:common_bloc/common_bloc.dart';

main(List<String> args) async {
  /// REST Bloc
  final RestBloc _restBloc = RestBloc('');

  ///Listen to changes
  _restBloc.listen((state) => state.when(
      uninitialized: () => print('Uninitialized'),
      error: (error) => print('Error: $error'),
      loading: () => print('Loading'),
      loaded: (data, lastPath, timestamp) => print('Loaded'),
      empty: () => print('Empty')));

  ///Get some values

  ///Close the stream

  /// Request Bloc
  final RequestBloc _requestBloc = RequestBloc();

  ///Listen to changes
  _requestBloc.listen((state) => state.when(
      uninitialized: () => print('Uninitialized'),
      error: (error) => print('Error: $error'),
      loading: () => print('Loading'),
      loaded: (data, lastAction, timestamp) => print('Loaded'),
      empty: () => print('Empty')));

  /// Perform a task
      () async => Future.delayed(Duration(seconds: 3), () => true),

  ///Close the stream

Use this package as a library

1. Depend on it

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

  common_bloc: ^0.1.2+1

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:common_bloc/common_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 27, 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


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.6.0 <3.0.0
bloc ^3.0.0 3.0.0
flutter 0.0.0
freezed_annotation ^0.7.1 0.7.1
http ^0.12.0+4 0.12.0+4
http_interceptor ^0.2.0 0.2.0
meta ^1.1.8 1.1.8
Transitive dependencies
async 2.4.1
charcode 1.1.3
collection 1.14.11 1.14.12
http_parser 3.1.4
json_annotation 3.0.1
path 1.6.4
pedantic 1.9.0
rxdart 0.23.1 0.24.0-dev.1
sky_engine 0.0.99
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
bloc_test ^3.1.0
build_runner ^1.7.4
freezed ^0.8.2