• This repository has been archived on 09/Jul/2020
  • Stars
    star
    189
  • Rank 204,649 (Top 5 %)
  • Language
    Dart
  • License
    MIT License
  • Created over 5 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

⚠️ Attention: This repository has been moved to https://github.com/felangel/bloc and is now read-only!

Hydrated Bloc

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

An extension to the bloc state management library which automatically persists and restores bloc states and is built on top of hydrated_cubit.

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
  Stream<int> mapEventToState(CounterEvent event) async* {
    switch (event) {
      case CounterEvent.decrement:
        yield state - 1;
        break;
      case CounterEvent.increment:
        yield state + 1;
        break;
    }
  }

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

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

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.

More Repositories

1

bloc

A predictable state management library that helps implement the BLoC design pattern
Dart
11,735
star
2

mason

Tools which allow developers to create and consume reusable templates called bricks.
Dart
967
star
3

equatable

A Dart package that helps to implement value based equality without needing to explicitly override == and hashCode.
Dart
784
star
4

mocktail

A mock library for Dart inspired by mockito
Dart
616
star
5

cubit

Cubit is a lightweight state management solution. It is a subset of the bloc package that does not rely on events and instead uses methods to emit new states.
Dart
583
star
6

flow_builder

Flutter Flows made easy! A Flutter package which simplifies navigation flows with a flexible, declarative API.
Dart
393
star
7

fresh

🍋 A token refresh library for Dart.
Dart
271
star
8

bloc.js

A predictable state management library that helps implement the BLoC design pattern in JavaScript
TypeScript
182
star
9

fluttersaurus

A Flutter Thesaurus made for Byteconf Flutter 2020
Dart
148
star
10

web_socket_client

A simple WebSocket client for Dart which includes automatic reconnection logic.
Dart
79
star
11

sealed_flutter_bloc

flutter_bloc state management extension that integrates sealed_unions.
Dart
70
star
12

flutter_hub

one-stop-shop to discover flutter projects, developers, and news
Dart
66
star
13

flutter_text_view_plugin

A simple flutter plugin that demonstrates how to use PlatformViews with Android to embed native views within the flutter widget tree.
Dart
43
star
14

felangel

31
star
15

bloc_library_basics_and_beyond

Bloc Library: Basics and Beyond - Flutter Europe Talk 2020
Dart
30
star
16

flutter_services_binding

A subset of WidgetsFlutterBinding specifically for initializing the ServicesBinding.
C++
27
star
17

inherited_stream

An inherited widget for Streams, which updates its dependencies when the stream emits data.
C++
22
star
18

cubit_and_beyond

Cubit and Beyond - Talk given at Flutter Vikings 2020
Dart
21
star
19

flutter_flows

A sample Flutter project which demonstrates how to use flow_builder. Part of talk given at Flutter Hub 2021
Dart
20
star
20

sealed_flutter_bloc_samples

Dart
20
star
21

rainbow_container

🌈 A magical container which changes colors whenever its build method is called.
C++
20
star
22

stream_listener

Stream Helpers for Flutter & Dart
Dart
20
star
23

meet_mason

Meet Mason: Introduction to Templating and Code Generation presented at Flutter Vikings 2022
18
star
24

cool_counter

A cool counter application which showcases HydratedCubit and ReplayCubit -- Flutter Warsaw 2020
Dart
16
star
25

broadcast_bloc

An extension to the bloc state management library which adds support for broadcasting state changes to stream channels.
Dart
16
star
26

hydrated_weather

An example of how to use hydrated_bloc to cache application state in a Weather App
Dart
15
star
27

struct

Experimental support for data classes in Dart using macros.
Dart
14
star
28

homebrew-mason

The official mason tap for homebrew
Ruby
13
star
29

codemagic_bloc_unit_tests

Bloc Unit Tests + Codemagic YAML
Dart
13
star
30

Flutter-Localization

Dart
9
star
31

simple_weather

A simple weather app built with flutter and the metaweather api.
Dart
7
star
32

bloc_todos

Dart
6
star
33

Flutter-Nested-TabBar

Dart
5
star
34

nested

A Flutter Widget which helps nest multiple widgets without needing to manually nest them.
Dart
4
star
35

flutter_platform_view_error

Ruby
3
star
36

flutter_skt_map_bug

Java
2
star
37

FelixAngelov

2
star
38

flutter_flavors_bug

C++
1
star
39

dartdevc_exception_bug

Dart
1
star
40

e2e_zones

C++
1
star
41

monorepo-ci-test

Dart
1
star
42

locale-manager

Locale-Manager is designed to be the simplest way possible to localize content while keeping an organized, scalable project structure.
JavaScript
1
star