@@ -4,13 +4,13 @@ import 'base_service.dart';
44
55/// A class implementing LocalStorageService interface using Hive as storage backend.
66class LocalStorageWithHive implements LocalStorageService {
7- final Map <String , Box > _collections = {};
7+ final Map <String , Future < Box > > _collections = {};
88
99 @override
10- Future < void > add (String collectionName) async {
10+ void add (String collectionName) {
1111 assert (! _collections.containsKey (collectionName), 'The Box "$collectionName " already exists' );
1212
13- _collections[collectionName] = await Hive .openBox (collectionName);
13+ _collections[collectionName] = Hive .openBox (collectionName);
1414 }
1515
1616 @override
@@ -20,9 +20,11 @@ class LocalStorageWithHive implements LocalStorageService {
2020 required String value,
2121 }) async {
2222 assert (_collections.containsKey (collectionName), 'The Box "$collectionName " does not exists.' );
23- assert (_collections[collectionName]! .containsKey (key), 'The key ($key ) already exists.' );
2423
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);
2628 }
2729
2830 @override
@@ -36,7 +38,8 @@ class LocalStorageWithHive implements LocalStorageService {
3638 Future <String ?> read ({String collectionName = '' , required String key}) async {
3739 assert (_collections.containsKey (collectionName), 'The Box "$collectionName " does not exists.' );
3840
39- String ? result = _collections[collectionName]! .get (key);
41+ Box box = await _collections[collectionName]! ;
42+ String ? result = box.get (key);
4043 return result;
4144 }
4245
@@ -45,8 +48,6 @@ class LocalStorageWithHive implements LocalStorageService {
4548 String collectionName = '' ,
4649 required List <String > keys,
4750 }) async {
48- assert (_collections.containsKey (collectionName), 'The Box "$collectionName " does not exists.' );
49-
5051 if (keys.isNotEmpty) {
5152 Map <String , String ?> result = {};
5253 for (String k in keys) {
@@ -62,9 +63,13 @@ class LocalStorageWithHive implements LocalStorageService {
6263 Future <Map <String , String ?>> readAll ({String collectionName = '' }) async {
6364 assert (_collections.containsKey (collectionName), 'The Box "$collectionName " does not exists.' );
6465
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+ );
6873 return result;
6974 }
7075
@@ -75,9 +80,11 @@ class LocalStorageWithHive implements LocalStorageService {
7580 required String value,
7681 }) async {
7782 assert (_collections.containsKey (collectionName), 'The Box "$collectionName " does not exists.' );
78- assert (! _collections[collectionName]! .containsKey (key), 'The key ($key ) does not exist.' );
7983
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);
8188 }
8289
8390 @override
@@ -95,7 +102,8 @@ class LocalStorageWithHive implements LocalStorageService {
95102 }) async {
96103 assert (_collections.containsKey (collectionName), 'The Box "$collectionName " does not exists.' );
97104
98- _collections[collectionName]! .put (key, value);
105+ Box box = await _collections[collectionName]! ;
106+ box.put (key, value);
99107 }
100108
101109 @override
@@ -112,22 +120,25 @@ class LocalStorageWithHive implements LocalStorageService {
112120 Future <void > delete ({String collectionName = '' , dynamic key}) async {
113121 assert (_collections.containsKey (collectionName), 'The Box "$collectionName " does not exists.' );
114122
115- await _collections[collectionName]! .delete (key);
123+ Box box = await _collections[collectionName]! ;
124+ await box.delete (key);
116125 }
117126
118127 @override
119128 Future <void > deleteMany ({String collectionName = '' , List <String > keys = const []}) async {
120129 assert (_collections.containsKey (collectionName), 'The Box "$collectionName " does not exists.' );
121130
131+ Box box = await _collections[collectionName]! ;
122132 if (keys.isNotEmpty) {
123- _collections[collectionName] ! .deleteAll (keys);
133+ await box .deleteAll (keys);
124134 }
125135 }
126136
127137 @override
128138 Future <void > deleteAll ({String collectionName = '' }) async {
129139 assert (_collections.containsKey (collectionName), 'The Box "$collectionName " does not exists.' );
130140
131- await _collections[collectionName]! .clear ();
141+ Box box = await _collections[collectionName]! ;
142+ await box.clear ();
132143 }
133144}
0 commit comments