easy_hive_encryption 1.1.0 easy_hive_encryption: ^1.1.0 copied to clipboard
Extension of easy_hive to support encryption.
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 🔐 #
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";
}
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