Skip to content

SoFluffyOS/easy_hive

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Easy Hive

Easy Hive is wrapper of Hive database for easier & simpler usage.

Outline πŸ“‹

Features 🎁

Easy 🦊
πŸ” Encryption βœ…
🐒 Lazy loading βœ…
πŸ”‘ Enum key support βœ…
🎧 Listenable βœ…

Installation πŸ’»

Add easy_hive to your pubspec.yaml:

dependencies:

  easy_hive: ^1.0.1+2

Install it:

flutter pub get

Usage πŸ“–

You can either define your boxes as Singleton classes or use a service locator like get_it.

1. Define box keys πŸ”‘

enum Settings {
  key, // Use as box key. You can use a String constant instead.

  /// Other keys below...
  themeMode,
  counter,
}

1. Define a box πŸ“¦

import 'package:easy_hive/easy_hive.dart';

class SettingsBox extends EasyBox {
  @override
  String get boxKey => Settings.key.toString();

  /// Singleton.
  static final SettingsBox _instance = SettingsBox._();

  factory SettingsBox() => _instance;

  SettingsBox._();
}
Or to use with get_it
import 'package:easy_hive/easy_hive.dart';

class SettingsBox extends EasyBox {
  @override
  String get boxKey => Settings.key.toString();
}

2. Initialize box πŸš€

import 'package:easy_hive/easy_hive.dart';

Future<void> main() async {
  await EasyBox.initialize();

  await SettingsBox().init();

  // runApp...
}
Or to use with get_it
import 'package:easy_hive/easy_hive.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Hive.initFlutter();

  final settingsBox = SettingsBox();
  await settingsBox.init();
  GetIt.I.registerSingleton<SettingsBox>(settingsBox);

  // runApp...
}

3. Define getter & setter for your data πŸ’„

extension GeneralSettingsExtension on SettingsBox {
  ThemeMode get themeMode {
    final index = get(
      Settings.themeMode,
      defaultValue: 0,
    );
    return ThemeMode.values[index];
  }

  set themeMode(ThemeMode value) => put(Settings.themeMode, value.index);

  int get counter => get(Settings.counter, defaultValue: 0);

  set counter(int value) => put(Settings.counter, value);
}

4. Use it anywhere πŸ”₯

  Text(
    'You have pushed: ${SettingsBox().counter} times.',
    style: Theme.of(context).textTheme.headlineMedium,
  ),
  FilledButton(
    onPressed: () {
      SettingsBox().counter++;
    },
    child: Text('Increment'),
  ),
  FilledButton(
    onPressed: () {
      SettingsBox().themeMode = ThemeMode.dark;
    },
    child: Text('Dark Theme'),
  ),
Or to use with get_it
  Text(
    'Count: ${GetIt.I<SettingsBox>().counter}',
    style: Theme.of(context).textTheme.headlineMedium,
  ),

Advanced Usage 😈

Enable encryption πŸ”

2. Add EncryptionMixin to your box class:

class SettingsBox extends EasyBox with EncryptionMixin {
  @override
  String get boxKey => Settings.key.toString();

  /// Override encryption key name (optional).
  @override
  String get encryptionKeyName => "your-own-key-name";
}

3. Follow flutter_secure_storage's guide for specific platform setup.

Enable lazy loading 🐒

1. Add LazyMixin to your box class:

class SettingsBox extends EasyBox with LazyMixin {
  @override
  String get boxKey => Settings.key.toString();
}

2. Use await to get your value:

extension GeneralSettingsExtension on SettingsBox {
  Future<ThemeMode> getThemeMode() async {
    final index = await get(
      Settings.themeMode,
      defaultValue: 0,
    );
    return ThemeMode.values[index];
  }
}

Listen to value changes 🎧

Recommended: Use RefreshableBox + provider:

1. Extends RefreshableBox instead of EasyBox:

class SettingsBox extends RefreshableBox {
  @override
  String get boxKey => Settings.key.toString();
}

2. Use it as a provider:

  ChangeNotifierProvider(
    create: (_) => SettingsBox(),
    child: SomeWidget(),
  ),
// Inside SomeWidget.
Text(
  'You have pushed: '
  '${context.select((SettingsBox _) => _.counter)} times.',
),

For more info, see provider package.


Or if you don't want RefreshableBox:

Just use ValueListenableBuilder to listen to changes.

ValueListenableBuilder(
  valueListenable: [
    Settings.counter,
  ].of(SettingsBox()),
  builder: (context, _, __) {
    return Text(
      '${SettingsBox().counter}',
    );
  },
),

Happy Coding 🦊

Made with ❀️ by Simon Pham