@@ -4,13 +4,13 @@ import 'base_service.dart';
4
4
5
5
/// A class implementing LocalStorageService interface using Hive as storage backend.
6
6
class LocalStorageWithHive implements LocalStorageService {
7
- final Map <String , Box > _collections = {};
7
+ final Map <String , Future < Box > > _collections = {};
8
8
9
9
@override
10
- Future < void > add (String collectionName) async {
10
+ void add (String collectionName) {
11
11
assert (! _collections.containsKey (collectionName), 'The Box "$collectionName " already exists' );
12
12
13
- _collections[collectionName] = await Hive .openBox (collectionName);
13
+ _collections[collectionName] = Hive .openBox (collectionName);
14
14
}
15
15
16
16
@override
@@ -20,9 +20,11 @@ class LocalStorageWithHive implements LocalStorageService {
20
20
required String value,
21
21
}) async {
22
22
assert (_collections.containsKey (collectionName), 'The Box "$collectionName " does not exists.' );
23
- assert (_collections[collectionName]! .containsKey (key), 'The key ($key ) already exists.' );
24
23
25
- await _collections[collectionName]! .put (key, value);
24
+ Box box = await _collections[collectionName]! ;
25
+ assert (! box.containsKey (key), 'The key "$key " already exists.' );
26
+
27
+ await box.put (key, value);
26
28
}
27
29
28
30
@override
@@ -36,7 +38,8 @@ class LocalStorageWithHive implements LocalStorageService {
36
38
Future <String ?> read ({String collectionName = '' , required String key}) async {
37
39
assert (_collections.containsKey (collectionName), 'The Box "$collectionName " does not exists.' );
38
40
39
- String ? result = _collections[collectionName]! .get (key);
41
+ Box box = await _collections[collectionName]! ;
42
+ String ? result = box.get (key);
40
43
return result;
41
44
}
42
45
@@ -45,8 +48,6 @@ class LocalStorageWithHive implements LocalStorageService {
45
48
String collectionName = '' ,
46
49
required List <String > keys,
47
50
}) async {
48
- assert (_collections.containsKey (collectionName), 'The Box "$collectionName " does not exists.' );
49
-
50
51
if (keys.isNotEmpty) {
51
52
Map <String , String ?> result = {};
52
53
for (String k in keys) {
@@ -62,9 +63,13 @@ class LocalStorageWithHive implements LocalStorageService {
62
63
Future <Map <String , String ?>> readAll ({String collectionName = '' }) async {
63
64
assert (_collections.containsKey (collectionName), 'The Box "$collectionName " does not exists.' );
64
65
65
- Map <String , String ?> result = _collections[collectionName]!
66
- .toMap ()
67
- .map ((key, value) => MapEntry (key.toString (), value? .toString ()));
66
+ Box box = await _collections[collectionName]! ;
67
+ Map <String , String ?> result = box.toMap ().map (
68
+ (key, value) => MapEntry (
69
+ key.toString (),
70
+ value? .toString (),
71
+ ),
72
+ );
68
73
return result;
69
74
}
70
75
@@ -75,9 +80,11 @@ class LocalStorageWithHive implements LocalStorageService {
75
80
required String value,
76
81
}) async {
77
82
assert (_collections.containsKey (collectionName), 'The Box "$collectionName " does not exists.' );
78
- assert (! _collections[collectionName]! .containsKey (key), 'The key ($key ) does not exist.' );
79
83
80
- _collections[collectionName]! .put (key, value);
84
+ Box box = await _collections[collectionName]! ;
85
+ assert (box.containsKey (key), 'The key "$key " does not exist.' );
86
+
87
+ box.put (key, value);
81
88
}
82
89
83
90
@override
@@ -95,7 +102,8 @@ class LocalStorageWithHive implements LocalStorageService {
95
102
}) async {
96
103
assert (_collections.containsKey (collectionName), 'The Box "$collectionName " does not exists.' );
97
104
98
- _collections[collectionName]! .put (key, value);
105
+ Box box = await _collections[collectionName]! ;
106
+ box.put (key, value);
99
107
}
100
108
101
109
@override
@@ -112,22 +120,25 @@ class LocalStorageWithHive implements LocalStorageService {
112
120
Future <void > delete ({String collectionName = '' , dynamic key}) async {
113
121
assert (_collections.containsKey (collectionName), 'The Box "$collectionName " does not exists.' );
114
122
115
- await _collections[collectionName]! .delete (key);
123
+ Box box = await _collections[collectionName]! ;
124
+ await box.delete (key);
116
125
}
117
126
118
127
@override
119
128
Future <void > deleteMany ({String collectionName = '' , List <String > keys = const []}) async {
120
129
assert (_collections.containsKey (collectionName), 'The Box "$collectionName " does not exists.' );
121
130
131
+ Box box = await _collections[collectionName]! ;
122
132
if (keys.isNotEmpty) {
123
- _collections[collectionName] ! .deleteAll (keys);
133
+ await box .deleteAll (keys);
124
134
}
125
135
}
126
136
127
137
@override
128
138
Future <void > deleteAll ({String collectionName = '' }) async {
129
139
assert (_collections.containsKey (collectionName), 'The Box "$collectionName " does not exists.' );
130
140
131
- await _collections[collectionName]! .clear ();
141
+ Box box = await _collections[collectionName]! ;
142
+ await box.clear ();
132
143
}
133
144
}
0 commit comments