Skip to content

Commit 2299443

Browse files
committed
Updated: LocalStorage, comments, async gap
Updated: documentation comments Fixed: async gap error
1 parent 9b398ac commit 2299443

File tree

5 files changed

+59
-29
lines changed

5 files changed

+59
-29
lines changed

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

+1-1
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

+1-1
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

+28-17
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

+1-1
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

+28-9
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,26 @@ abstract class LocalStorage {
2424
///
2525
/// It's not required in the case of [LocalStorageWithFlutterSecureStorage].
2626
///
27+
/// Throws an assertion error if [collectionName] exists already.
28+
///
2729
/// Example:
2830
/// ```dart
2931
/// LocalStorage.add('posts');
3032
/// //used only with Hive
3133
/// ```
32-
Future<void> add(String collectionName) {
34+
void add(String collectionName) {
3335
return _instanceLocal.add(collectionName);
3436
}
3537

3638
/// Creates a new item in the [collectionName] of [LocalStorage].
3739
///
3840
/// To create a single key-value pair pass the [key] and the [value] as String, the
3941
/// String could be a JSON String or a simple text according to your requirement.
40-
/// If the key is already present in the [LocalStorage] an assertion error will be thrown.
42+
///
43+
/// Throws an assertion error if the key is already present in the [LocalStorage] or
44+
/// [collectionName] is not added.
45+
///
46+
/// Try [LocalStorage.createOrUpdate], [LocalStorage.update].
4147
///
4248
/// Example:
4349
///
@@ -60,8 +66,11 @@ abstract class LocalStorage {
6066
/// Creates new items in the[collectionName] of [LocalStorage].
6167
/// If you want to create multiple entries pass the [values] as a Map<String, String>, then it
6268
/// will create all the key-value pairs from the [values] map.
63-
/// If any key from the [values] is already present in the [LocalStorage] an assertion error will
64-
/// be thrown.
69+
///
70+
/// Throws an assertion error if the key is already present in the [LocalStorage] or
71+
/// [collectionName] is not added.
72+
///
73+
/// Try [LocalStorage.createOrUpdateMany], [LocalStorage.updateMany].
6574
///
6675
/// Example:
6776
///
@@ -101,7 +110,10 @@ abstract class LocalStorage {
101110
/// Reads the value of the item at [key] from the [LocalStorage] and returns the value.
102111
///
103112
/// Read a single value by passing [key] as String, it will return the value as String?.
104-
/// If the [key] already exists an assertion error will be thrown.
113+
///
114+
/// Throws an assertion error if [key] already exists or [collectionName] is not added.
115+
///
116+
/// Try [LocalStorage.createOrUpdateMany], [LocalStorage.updateMany].
105117
///
106118
/// Example:
107119
/// ```dart
@@ -148,7 +160,11 @@ abstract class LocalStorage {
148160

149161
/// Updates an item in [collectionName] of the [LocalStorage].
150162
///
151-
/// To update a single key-value pair pass the [key] and the [value] as String, the
163+
/// To update a single key-value pair pass the [key] and the [value] as String.
164+
///
165+
/// Throws asserstion error if [key] does not exists or [collectionName] is not added.
166+
///
167+
/// Try [LocalStorage.createOrUpdate], [LocalStorage.create].
152168
///
153169
/// Example:
154170
/// ```dart
@@ -167,6 +183,10 @@ abstract class LocalStorage {
167183
/// If you want to update multiple entries pass the [values] as a Map<String, String>, then it
168184
/// will update all the key-value pairs in the [values] map.
169185
///
186+
/// Throws asserstion error if [key] does not exists or [collectionName] is not added.
187+
///
188+
/// Try [LocalStorage.createOrUpdateMany], [LocalStorage.createMany].
189+
///
170190
/// Example:
171191
/// ```dart
172192
/// await LocalStorage.updateMany(
@@ -191,8 +211,7 @@ abstract class LocalStorage {
191211

192212
/// Creates or updates an item in [collectionName] of [LocalStorage].
193213
///
194-
/// To create or update a single key-value pair pass the [key] and the [value] as String, the
195-
/// If the [key] is already present in the [LocalStorage] it's value will be overwritten.
214+
/// To create or update a single key-value pair pass the [key] and the [value] as String.
196215
///
197216
/// Example:
198217
/// ```dart
@@ -210,7 +229,7 @@ abstract class LocalStorage {
210229
/// Creates or updates items in [collectionName] of [LocalStorage].
211230
/// If you want to create or update multiple entries pass the [values] as a Map<String, String>, then it
212231
/// will create all the key-value pairs from the [values] map.
213-
/// If any key from the [values] is already present in the [LocalStorage] it's value will be
232+
/// If any key from the [values] is already present in the [collectionName] of [LocalStorage] it's value will be
214233
/// overwritten.
215234
///
216235
/// Example:

0 commit comments

Comments
 (0)