hydrated_bloc 5.0.0-dev.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 93

Hydrated Bloc

Pub Version Build Status Code Coverage style: effective dart MIT License Starware

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

Overview #

hydrated_bloc exports a Storage interface which means it can work with any storage provider. Out of the box, it comes with its own implementation: HydratedStorage.

HydratedStorage is built on top of path_provider for a platform-agnostic storage layer. The out-of-the-box storage implementation reads/writes to file using the toJson/fromJson methods on HydratedBloc and should perform very well for most use-cases (performance reports coming soon). HydratedStorage is supported for desktop (example).

Usage #

1. Use HydratedStorage #

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  HydratedBloc.storage = await HydratedStorage.build();
  runApp(App());
}

2. Extend HydratedBloc and override fromJson/toJson #

enum CounterEvent { increment, decrement }

class CounterBloc extends HydratedBloc<CounterEvent, int> {
  CounterBloc() : super(0);

  @override
  int fromJson(Map<String, dynamic> json) => json['value'] as int;

  @override
  Map<String, int> toJson(int state) => { 'value': state };

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

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.

Custom Storage Directory #

By default, all data is written to temporary storage which means it can be wiped by the operating system at any point in time.

An optional storageDirectory can be provided to override the default temporary storage directory:

HydratedBloc.storage = await HydratedStorage.build(
  storageDirectory: await getApplicationDocumentsDirectory(),
);

Custom Hydrated Storage #

If the default HydratedStorage doesn't meet your needs, you can always implement a custom Storage by simply implementing the Storage interface and initializing HydratedBloc with the custom Storage.

// my_hydrated_storage.dart

class MyHydratedStorage implements Storage {
  @override
  dynamic read(String key) {
    // TODO: implement read
  }

  @override
  Future<void> write(String key, dynamic value) async {
    // TODO: implement write
  }

  @override
  Future<void> delete(String key) async {
    // TODO: implement delete
  }

  @override
  Future<void> clear() async {
    // TODO: implement clear
  }
}
// main.dart

HydratedBloc.storage = MyHydratedStorage();

Maintainers #

Supporters #

Starware #

Hydrated Bloc is Starware.
This means you're free to use the project, as long as you star its GitHub repository.
Your appreciation makes us grow and glow up. ⭐

5.0.0-dev.2 #

  • BREAKING: update to bloc ^5.0.0-dev.10
  • BREAKING: extend hydrated_cubit ^0.0.3

5.0.0-dev.1 #

  • BREAKING: update to bloc ^5.0.0-dev.7
  • BREAKING: super.initialState is no longer required

4.1.1 #

  • Remove unnecessary print statement

4.1.0 #

  • Update default HydratedStorage to use package:hive (thanks to @orsenkucher).
  • Add encryption support to HydratedStorage (thanks to @orsenkucher).

4.0.0 #

  • Updated to bloc: ^4.0.0 and flutter_bloc: ^4.0.0
  • onTransition moved from HydratedBlocDelegate to HydratedBloc

3.1.0 #

  • Persist initialState when initialized (thanks to @orsenkucher).
  • Fix: add synchronized to prevent file corruption (thanks to @orsenkucher)
  • Refactor HydratedBlocStorage.getInstance to avoid using singleton (thanks to @orsenkucher)
  • Upgrade to path_provider: ^1.6.5
  • Fix: invoke onError and continue emitting states when exceptions occur

3.0.0 #

  • Updated to bloc: ^3.0.0

3.0.0-dev.1 #

  • Updated to bloc: ^3.0.0-dev.1

2.0.0 #

1.1.0 #

  • Optional storageDirectory can be provided (#28).
  • Documentation Updates

1.0.0 #

  • Update to bloc v1.0.0
  • Documentation Updates

0.8.0 #

  • Update to bloc v0.16.0
  • Documentation Updates

0.7.0 #

0.6.0 #

  • Support clearing individual HydratedBloc caches (#21)
  • Documentation and Example Updates

0.5.0 #

  • Support for Desktop (#18)
  • Documentation and Example Updates

0.4.1 #

  • Update to support optional id in cases where there are multiple instances of the same HydratedBloc
  • Documentation Updates

0.4.0 #

  • Update to bloc v0.15.0
  • Documentation Updates

0.3.2 #

  • Minor Updates to Package Dependencies
  • Documentation Updates

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/lib/main.dart

import 'dart:async';

import 'package:flutter/material.dart';

import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hydrated_bloc/hydrated_bloc.dart';

void main() async {
  // https://github.com/flutter/flutter/pull/38464
  // Changes in Flutter v1.9.4 require you to call WidgetsFlutterBinding.ensureInitialized()
  // before using any plugins if the code is executed before runApp.
  // As a result, you will need the following line if you're using Flutter >=1.9.4.
  WidgetsFlutterBinding.ensureInitialized();
  HydratedBloc.storage = await HydratedStorage.build();
  runApp(App());
}

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocProvider<CounterBloc>(
      create: (_) => CounterBloc(),
      child: MaterialApp(home: CounterPage()),
    );
  }
}

class CounterPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final textTheme = Theme.of(context).textTheme;
    return Scaffold(
      appBar: AppBar(title: const Text('Counter')),
      body: BlocBuilder<CounterBloc, int>(
        builder: (BuildContext context, int state) {
          return Center(
            child: Text('$state', style: textTheme.headline2),
          );
        },
      ),
      floatingActionButton: Column(
        crossAxisAlignment: CrossAxisAlignment.end,
        mainAxisAlignment: MainAxisAlignment.end,
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 5.0),
            child: FloatingActionButton(
              child: const Icon(Icons.add),
              onPressed: () {
                context.bloc<CounterBloc>().add(CounterEvent.increment);
              },
            ),
          ),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 5.0),
            child: FloatingActionButton(
              child: const Icon(Icons.remove),
              onPressed: () {
                context.bloc<CounterBloc>().add(CounterEvent.decrement);
              },
            ),
          ),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 5.0),
            child: FloatingActionButton(
              child: const Icon(Icons.delete_forever),
              onPressed: () async {
                final counterBloc = context.bloc<CounterBloc>();
                await counterBloc.clear();
                counterBloc.add(CounterEvent.reset);
              },
            ),
          ),
        ],
      ),
    );
  }
}

enum CounterEvent { increment, decrement, reset }

class CounterBloc extends HydratedBloc<CounterEvent, int> {
  CounterBloc() : super(0);

  @override
  Stream<int> mapEventToState(CounterEvent event) async* {
    switch (event) {
      case CounterEvent.decrement:
        yield state - 1;
        break;
      case CounterEvent.increment:
        yield state + 1;
        break;
      case CounterEvent.reset:
        yield 0;
        break;
    }
  }

  @override
  int fromJson(Map<String, dynamic> json) => json['value'] as int;

  @override
  Map<String, int> toJson(int state) => {'value': state};
}

Use this package as a library

1. Depend on it

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


dependencies:
  hydrated_bloc: ^5.0.0-dev.2

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]
93
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
85
Overall:
Weighted score of the above. [more]
93
Learn more about scoring.

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

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform android

Because of import path [package:hydrated_bloc/hydrated_bloc.dart, package:hydrated_bloc/src/hydrated_bloc.dart, package:hydrated_cubit/hydrated_cubit.dart, package:hydrated_cubit/src/hydrated_storage.dart, package:path_provider/path_provider.dart, package:path_provider_linux/path_provider_linux.dart] that declares support for platforms: linux

Package does not support Flutter platform ios

Because of import path [package:hydrated_bloc/hydrated_bloc.dart, package:hydrated_bloc/src/hydrated_bloc.dart, package:hydrated_cubit/hydrated_cubit.dart, package:hydrated_cubit/src/hydrated_storage.dart, package:path_provider/path_provider.dart, package:path_provider_linux/path_provider_linux.dart] that declares support for platforms: linux

Package does not support Flutter platform macos

Because of import path [package:hydrated_bloc/hydrated_bloc.dart, package:hydrated_bloc/src/hydrated_bloc.dart, package:hydrated_cubit/hydrated_cubit.dart, package:hydrated_cubit/src/hydrated_storage.dart, package:path_provider/path_provider.dart, package:path_provider_linux/path_provider_linux.dart] that declares support for platforms: linux

Package does not support Flutter platform web

Because of import path [package:hydrated_bloc/hydrated_bloc.dart, package:hydrated_bloc/src/hydrated_bloc.dart, package:hydrated_cubit/hydrated_cubit.dart, package:hydrated_cubit/src/hydrated_storage.dart, package:path_provider/path_provider.dart] that declares support for platforms: android, ios, linux, macos

Package does not support Flutter platform windows

Because of import path [package:hydrated_bloc/hydrated_bloc.dart, package:hydrated_bloc/src/hydrated_bloc.dart, package:hydrated_cubit/hydrated_cubit.dart, package:hydrated_cubit/src/hydrated_storage.dart, package:path_provider/path_provider.dart] that declares support for platforms: android, ios, linux, macos

Package not compatible with SDK dart

because of import path [hydrated_bloc] that is in a package requiring null.

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 (hydrated_cubit).

Package is pre-release. (-5 points)

Pre-release versions should be used with caution; their API can change in breaking ways.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
bloc ^5.0.0-dev.10 5.0.0-dev.11 5.0.0
flutter 0.0.0
hive ^1.4.1+1 1.4.1+1
hydrated_cubit ^0.0.3 0.0.3 0.1.0
meta ^1.1.8 1.1.8
path_provider ^1.6.5 1.6.11
synchronized ^2.2.0 2.2.0+1
Transitive dependencies
charcode 1.1.3
collection 1.14.12 1.14.13
convert 2.1.1
cubit 0.0.14 0.1.0
file 5.2.1
intl 0.16.1
path 1.7.0
path_provider_linux 0.0.1+2
path_provider_macos 0.0.4+3
path_provider_platform_interface 1.0.2
platform 2.2.1
plugin_platform_interface 1.0.2
process 3.0.13
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8
xdg_directories 0.1.0
Dev dependencies
crypto ^2.1.4 2.1.5
effective_dart ^1.1.1
flutter_test
mockito ^4.0.0
uuid ^2.0.4