Aidra Connect 10.0.2+16
Aidra Connect Mobile Application
Loading...
Searching...
No Matches
hive_services.dart
Go to the documentation of this file.
1import 'package:hive/hive.dart';
2
4 final String boxName;
5 Box<T>? _box;
6
7 HiveCrudService(this.boxName);
8
9 Future<void> init() async {
10 if (!Hive.isBoxOpen(boxName)) {
11 _box = await Hive.openBox<T>(boxName);
12 } else {
13 _box = Hive.box<T>(boxName);
14 }
15 }
16
17 Future<Box<T>> _getBox() async {
18 if (_box == null || !_box!.isOpen) {
19 await init();
20 }
21 return _box!;
22 }
23
24 Future<void> put(String key, T value) async {
25 final box = await _getBox();
26 await box.put(key, value);
27 }
28
29 Future<void> addAll(List<T> values) async {
30 final box = await _getBox();
31 for (var value in values) {
32 await box.add(value);
33 }
34 }
35
36 Future<T?> get(String key) async {
37 final box = await _getBox();
38 return box.get(key);
39 }
40
41 Future<List<T>> getAll() async {
42 final box = await _getBox();
43 return box.values.toList();
44 }
45
46 Future<void> delete(String key) async {
47 final box = await _getBox();
48 await box.delete(key);
49 }
50
51 Future<void> clear() async {
52 final box = await _getBox();
53 await box.clear();
54 }
55
56 Future<bool> containsKey(String key) async {
57 final box = await _getBox();
58 return box.containsKey(key);
59 }
60
61 Future<void> closeBox() async {
62 if (_box != null && _box!.isOpen) {
63 await _box!.close();
64 }
65 }
66}
Future< T?> get(String key) async
Future< void > put(String key, T value) async
Future< void > closeBox() async
Future< void > addAll(List< T > values) async
Future< void > clear() async
Future< void > init() async
Future< List< T > > getAll() async
HiveCrudService(this.boxName)
Future< bool > containsKey(String key) async
Future< Box< T > > _getBox() async
final double value
Future< void > init() async