Easy Hive is wrapper of Hive database for easier & simpler usage.
Easy | π¦ |
---|---|
π Encryption | β |
π’ Lazy loading | β |
π Enum key support | β |
π§ Listenable | β |
Add easy_hive
to your pubspec.yaml
:
dependencies:
easy_hive: ^1.0.1+2
Install it:
flutter pub get
You can either define your boxes as Singleton classes or use a service locator like get_it.
enum Settings {
key, // Use as box key. You can use a String constant instead.
/// Other keys below...
themeMode,
counter,
}
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();
}
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...
}
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);
}
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,
),
1. Install easy_hive_encryption:
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.
class SettingsBox extends EasyBox with LazyMixin {
@override
String get boxKey => Settings.key.toString();
}
extension GeneralSettingsExtension on SettingsBox {
Future<ThemeMode> getThemeMode() async {
final index = await get(
Settings.themeMode,
defaultValue: 0,
);
return ThemeMode.values[index];
}
}
Recommended: Use RefreshableBox
+ provider:
class SettingsBox extends RefreshableBox {
@override
String get boxKey => Settings.key.toString();
}
ChangeNotifierProvider(
create: (_) => SettingsBox(),
child: SomeWidget(),
),
// Inside SomeWidget.
Text(
'You have pushed: '
'${context.select((SettingsBox _) => _.counter)} times.',
),
For more info, see provider package.
ValueListenableBuilder(
valueListenable: [
Settings.counter,
].of(SettingsBox()),
builder: (context, _, __) {
return Text(
'${SettingsBox().counter}',
);
},
),
Made with β€οΈ by Simon Pham