Skip to content

Commit 3e72079

Browse files
committed
Fixed: async gap error
1 parent 8912080 commit 3e72079

File tree

5 files changed

+32
-21
lines changed

5 files changed

+32
-21
lines changed

lib/vaahextendflutter/services/storage/local/services/base_service.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
abstract class LocalStorageService {
2-
Future<void> add(String collectionName);
2+
void add(String collectionName);
33

44
Future<void> create({String collectionName, required String key, required String value});
55

lib/vaahextendflutter/services/storage/local/services/flutter_secure_storage.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class LocalStorageWithFlutterSecureStorage implements LocalStorageService {
88
final _storage = const FlutterSecureStorage();
99

1010
@override
11-
Future<void> add(String name) async {}
11+
void add(String name) {}
1212

1313
@override
1414
Future<void> create({

lib/vaahextendflutter/services/storage/local/services/hive.dart

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import 'base_service.dart';
44

55
/// A class implementing LocalStorageService interface using Hive as storage backend.
66
class 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
}

lib/vaahextendflutter/services/storage/local/services/no_op_storage.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import 'base_service.dart';
33
/// A placeholder storage class when [LocalStorageType.none] is selected in env.dart.
44
class NoOpStorage implements LocalStorageService {
55
@override
6-
Future<void> add(String name) async {}
6+
void add(String name) {}
77

88
@override
99
Future<void> create({

lib/vaahextendflutter/services/storage/local/storage.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ abstract class LocalStorage {
3131
/// LocalStorage.add('posts');
3232
/// //used only with Hive
3333
/// ```
34-
Future<void> add(String collectionName) {
34+
void add(String collectionName) {
3535
return _instanceLocal.add(collectionName);
3636
}
3737

0 commit comments

Comments
 (0)