hydrated_bloc 0.3.1

logo
Build Status Code Coverage Pub Package MIT License


An extension to the bloc state management library which automatically persists and restores bloc states.

Usage #

1. Use HydratedBlocDelegate

void main() async {
  BlocSupervisor.delegate = await HydratedBlocDelegate.build();
  runApp(App());
}

2. Extend HydratedBloc and override initialState, fromJson and toJson methods

class CounterBloc extends HydratedBloc<CounterEvent, CounterState> {
  // You need to update state getter so it will get initialState
  // from storage when it is already populated/loaded and parsed 
  // without errors
  @override
  CounterState get initialState {
    return super.initialState ?? CounterState(0);
  }

  // This method is used to parse state object when retrieving its
  // saved JSON version from HydratedBloc internal storage.
  // Internally it is enclosed in try-catch block, so, to avoid
  // unexpected errors its better to thoroughly test method
  // somewhere else/enclose whole body and handle exceptions or
  // errors inside.
  @override
  CounterState fromJson(Map<String, dynamic> source) {
    return CounterState(source['value'] as int);
  }

  // Method which is used to convert bloc's state into JSON
  @override
  Map<String, int> toJson(CounterState state) {
    return {'value': state.value};
  }

  @override
  Stream<CounterState> mapEventToState(CounterEvent event) async* {
    switch (event) {
      case CounterEvent.decrement:
        yield CounterState(currentState.value - 1);
        break;
      case CounterEvent.increment:
        yield CounterState(currentState.value + 1);
        break;
    }
  }
}

enum CounterEvent { increment, decrement }

class CounterState {
  int value;

  CounterState(this.value);
}

Now our CounterBloc is a HydratedBloc and will automatically persist its state. We can increment the counter value, hot restart, kill the app, etc... and our CounterBloc will always retain its state.

0.3.1 #

  • Add guards to HydratedBlocStorage to prevent exception if cache is corrupt.

0.3.0 #

  • Update HydratedBlocStorage to use getTemporaryDirectory instead of getApplicationDocumentsDirectory
  • Documentation Updates

0.2.1 #

  • Bugfix to handle Blocs alongside HydrateBlocs within the same application.
  • toJson can return null to avoid persisting the state change

0.2.0 #

  • Upated HydrateBlocDelegate to have a static build
  • Updated toJson and fromJson to eliminate the need to call json.encode and json.decode explicitly.
  • HydratedBlocSharedPreferences replaced with HydratedBlocStorage
  • Removed dependency on SharedPreferences
  • Documentation Updates

0.1.0 #

  • Renamed HydratedBlocSharedPreferences to HydratedSharedPreferences
  • Documentation Updates

0.0.3 #

Added clear to HydratedBlocStorage API and Documentation Updates

0.0.2 #

Documentation Updates

0.0.1 #

Initial Version of the library.

Includes:

  • HydratedBloc
  • HydratedBlocDelegate
  • HydratedBlocSharedPreferences

example/README.md

example #

A new Flutter project.

Getting Started #

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Use this package as a library

1. Depend on it

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


dependencies:
  hydrated_bloc: ^0.3.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:hydrated_bloc/hydrated_bloc.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
80
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
88
Learn more about scoring.

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

  • Dart: 2.4.0
  • pana: 0.12.19
  • Flutter: 1.7.8+hotfix.3

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (meta).

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
bloc ^0.14.0 0.14.4
flutter 0.0.0
meta ^1.1.6 1.1.6 1.1.7
path_provider ^1.1.0 1.1.2
Transitive dependencies
collection 1.14.11
rxdart 0.22.0
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_bloc ^0.19.0
flutter_test
mockito ^4.0.0

Admin