hydrated_cubit 0.1.4

  • Readme
  • Changelog
  • Example
  • Installing
  • new93

Cubit

Pub build coverage Star on GitHub Discord License: MIT Starware

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

Creating a HydratedCubit #

class CounterCubit extends HydratedCubit<int> {
  CounterCubit() : super(0);

  void increment() => emit(state + 1);
  void decrement() => emit(state - 1);

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

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

Using a HydratedCubit #

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  HydratedCubit.storage = await HydratedStorage.build();
  final counterCubit = CounterCubit();
}

Dart Versions #

  • Dart 2: >= 2.7.0

Maintainers #

Supporters #

Very Good Ventures

Starware #

HydratedCubit 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. ⭐

0.1.4 #

  • docs: inline documentation improvements
  • fix: upgrade to cubit: ^0.1.2

0.1.3 #

  • fix: excessive storage reads and fromJson invocations
  • docs: minor documentation improvements

0.1.2 #

  • fix: reintroduce migration code to ensure no data loss (#67)

0.1.1 #

  • fix: support use alongside hive (#53)

0.1.0 #

  • feat: upgrade to cubit: ^0.1.0

0.0.3 #

  • BREAKING:
    • Rename HydratedStorage to Storage
    • Rename HydratedCubitStorage to HydratedStorage
    • Rename getInstance to build

0.0.2 #

  • docs: minor improvements to README

0.0.1 #

  • feat: initial release

example/lib/main.dart

import 'package:flutter/material.dart';

import 'package:flutter_cubit/flutter_cubit.dart';
import 'package:hydrated_cubit/hydrated_cubit.dart';

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

/// A `StatelessWidget` which uses:
/// * [hydrated_cubit](https://pub.dev/packages/hydrated_cubit)
/// * [flutter_cubit](https://pub.dev/packages/flutter_cubit)
/// to manage the state of a counter.
class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CubitProvider<CounterCubit>(
      create: (_) => CounterCubit(),
      child: MaterialApp(home: CounterPage()),
    );
  }
}

/// A [StatelessWidget] which demonstrates
/// how to consume and interact with a [HydratedCubit].
class CounterPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final textTheme = Theme.of(context).textTheme.headline2;
    return Scaffold(
      appBar: AppBar(title: const Text('Counter')),
      body: CubitBuilder<CounterCubit, int>(
        builder: (BuildContext context, int state) {
          return Center(child: Text('$state', style: textTheme));
        },
      ),
      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.cubit<CounterCubit>().increment(),
            ),
          ),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 5.0),
            child: FloatingActionButton(
              child: const Icon(Icons.remove),
              onPressed: () => context.cubit<CounterCubit>().decrement(),
            ),
          ),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 5.0),
            child: FloatingActionButton(
              child: const Icon(Icons.delete_forever),
              onPressed: () async {
                final counterCubit = context.cubit<CounterCubit>();
                await counterCubit.clear();
                counterCubit.reset();
              },
            ),
          ),
        ],
      ),
    );
  }
}

/// {@template hydrated_counter_cubit}
/// A simple [HydratedCubit] which manages an `int` as its state
/// and exposes three public methods to `increment`, `decrement`, and
/// `reset` the value of the state.
/// {@endtemplate}
class CounterCubit extends HydratedCubit<int> {
  /// {@macro hydrated_counter_cubit}
  CounterCubit() : super(0);

  /// Increments the `cubit` state by 1.
  void increment() => emit(state + 1);

  /// Decrements the `cubit` state by 1.
  void decrement() => emit(state - 1);

  /// Resets the `cubit` state to 0.
  void reset() => emit(0);

  @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_cubit: ^0.1.4

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

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

  • Dart: 2.8.4
  • pana: 0.13.15
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform Android

Because:

  • package:hydrated_cubit/hydrated_cubit.dart that imports:
  • package:hydrated_cubit/src/hydrated_storage.dart that imports:
  • package:path_provider/path_provider.dart that imports:
  • package:path_provider_linux/path_provider_linux.dart that declares support for platforms: Linux

Package does not support Flutter platform Web

Because:

  • package:hydrated_cubit/hydrated_cubit.dart that imports:
  • package:hydrated_cubit/src/hydrated_storage.dart that imports:
  • package:path_provider/path_provider.dart that declares support for platforms: Android, iOS, Linux, macOS

Package does not support Flutter platform Windows

Because:

  • package:hydrated_cubit/hydrated_cubit.dart that imports:
  • package:hydrated_cubit/src/hydrated_storage.dart that imports:
  • package:path_provider/path_provider.dart that declares support for platforms: Android, iOS, Linux, macOS

Package does not support Flutter platform iOS

Because:

  • package:hydrated_cubit/hydrated_cubit.dart that imports:
  • package:hydrated_cubit/src/hydrated_storage.dart that imports:
  • package:path_provider/path_provider.dart that imports:
  • package:path_provider_linux/path_provider_linux.dart that declares support for platforms: Linux

Package does not support Flutter platform macOS

Because:

  • package:hydrated_cubit/hydrated_cubit.dart that imports:
  • package:hydrated_cubit/src/hydrated_storage.dart that imports:
  • package:path_provider/path_provider.dart that imports:
  • package:path_provider_linux/path_provider_linux.dart that declares support for platforms: Linux

Package not compatible with SDK dart

Because:

  • hydrated_cubit that is a package requiring null.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
cubit ^0.1.2 0.1.2 0.2.0-dev.1
flutter 0.0.0
hive ^1.4.1+1 1.4.1+1
meta ^1.1.8 1.1.8 1.2.2
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
file 5.2.1
intl 0.16.1
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 2.1.0-nullsafety
xdg_directories 0.1.0
Dev dependencies
crypto ^2.1.4 2.1.5
flutter_test
mockito ^4.0.0
path ^1.6.4 1.7.0
pedantic ^1.9.0
uuid ^2.0.4