0 purchases
easy hive encryption
Easy Hive #
Easy Hive is wrapper of Hive database for
easier & simpler usage.
Outline 📋 #
Features
Installation
Usage
Advanced Usage
Features 🎁 #
Easy
🦊
🔐 Encryption
✅
🐢 Lazy loading
✅
🔑 Enum key support
✅
🎧 Listenable
✅
Installation 💻 #
Add easy_hive to your pubspec.yaml:
dependencies:
easy_hive: ^1.0.1+2
copied to clipboard
Install it:
flutter pub get
copied to clipboard
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,
}
copied to clipboard
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._();
}
copied to clipboard
Or to use with get_it
import 'package:easy_hive/easy_hive.dart';
class SettingsBox extends EasyBox {
@override
String get boxKey => Settings.key.toString();
}
copied to clipboard
2. Initialize box 🚀 #
import 'package:easy_hive/easy_hive.dart';
Future<void> main() async {
await EasyBox.initialize();
await SettingsBox().init();
// runApp...
}
copied to clipboard
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...
}
copied to clipboard
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);
}
copied to clipboard
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'),
),
copied to clipboard
Or to use with get_it
Text(
'Count: ${GetIt.I<SettingsBox>().counter}',
style: Theme.of(context).textTheme.headlineMedium,
),
copied to clipboard
Advanced Usage 😈 #
Enable encryption 🔐 #
1. Install easy_hive_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";
}
copied to clipboard
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();
}
copied to clipboard
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];
}
}
copied to clipboard
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();
}
copied to clipboard
2. Use it as a provider:
ChangeNotifierProvider(
create: (_) => SettingsBox(),
child: SomeWidget(),
),
copied to clipboard
// Inside SomeWidget.
Text(
'You have pushed: '
'${context.select((SettingsBox _) => _.counter)} times.',
),
copied to clipboard
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}',
);
},
),
copied to clipboard
Happy Coding 🦊 #
Made with ❤️ by Simon Pham
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.