Skip to content

Commit

Permalink
Fixed: async gap error
Browse files Browse the repository at this point in the history
  • Loading branch information
we-mohd-i001 committed Jun 14, 2024
1 parent 8912080 commit 3e72079
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
abstract class LocalStorageService {
Future<void> add(String collectionName);
void add(String collectionName);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class LocalStorageWithFlutterSecureStorage implements LocalStorageService {
final _storage = const FlutterSecureStorage();

@override
Future<void> add(String name) async {}
void add(String name) {}

@override
Future<void> create({
Expand Down
45 changes: 28 additions & 17 deletions lib/vaahextendflutter/services/storage/local/services/hive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import 'base_service.dart';

/// A class implementing LocalStorageService interface using Hive as storage backend.
class LocalStorageWithHive implements LocalStorageService {
final Map<String, Box> _collections = {};
final Map<String, Future<Box>> _collections = {};

@override
Future<void> add(String collectionName) async {
void add(String collectionName) {
assert(!_collections.containsKey(collectionName), 'The Box "$collectionName" already exists');

_collections[collectionName] = await Hive.openBox(collectionName);
_collections[collectionName] = Hive.openBox(collectionName);
}

@override
Expand All @@ -20,9 +20,11 @@ class LocalStorageWithHive implements LocalStorageService {
required String value,
}) async {
assert(_collections.containsKey(collectionName), 'The Box "$collectionName" does not exists.');
assert(_collections[collectionName]!.containsKey(key), 'The key ($key) already exists.');

await _collections[collectionName]!.put(key, value);
Box box = await _collections[collectionName]!;
assert(!box.containsKey(key), 'The key "$key" already exists.');

await box.put(key, value);
}

@override
Expand All @@ -36,7 +38,8 @@ class LocalStorageWithHive implements LocalStorageService {
Future<String?> read({String collectionName = '', required String key}) async {
assert(_collections.containsKey(collectionName), 'The Box "$collectionName" does not exists.');

String? result = _collections[collectionName]!.get(key);
Box box = await _collections[collectionName]!;
String? result = box.get(key);
return result;
}

Expand All @@ -45,8 +48,6 @@ class LocalStorageWithHive implements LocalStorageService {
String collectionName = '',
required List<String> keys,
}) async {
assert(_collections.containsKey(collectionName), 'The Box "$collectionName" does not exists.');

if (keys.isNotEmpty) {
Map<String, String?> result = {};
for (String k in keys) {
Expand All @@ -62,9 +63,13 @@ class LocalStorageWithHive implements LocalStorageService {
Future<Map<String, String?>> readAll({String collectionName = ''}) async {
assert(_collections.containsKey(collectionName), 'The Box "$collectionName" does not exists.');

Map<String, String?> result = _collections[collectionName]!
.toMap()
.map((key, value) => MapEntry(key.toString(), value?.toString()));
Box box = await _collections[collectionName]!;
Map<String, String?> result = box.toMap().map(
(key, value) => MapEntry(
key.toString(),
value?.toString(),
),
);
return result;
}

Expand All @@ -75,9 +80,11 @@ class LocalStorageWithHive implements LocalStorageService {
required String value,
}) async {
assert(_collections.containsKey(collectionName), 'The Box "$collectionName" does not exists.');
assert(!_collections[collectionName]!.containsKey(key), 'The key ($key) does not exist.');

_collections[collectionName]!.put(key, value);
Box box = await _collections[collectionName]!;
assert(box.containsKey(key), 'The key "$key" does not exist.');

box.put(key, value);
}

@override
Expand All @@ -95,7 +102,8 @@ class LocalStorageWithHive implements LocalStorageService {
}) async {
assert(_collections.containsKey(collectionName), 'The Box "$collectionName" does not exists.');

_collections[collectionName]!.put(key, value);
Box box = await _collections[collectionName]!;
box.put(key, value);
}

@override
Expand All @@ -112,22 +120,25 @@ class LocalStorageWithHive implements LocalStorageService {
Future<void> delete({String collectionName = '', dynamic key}) async {
assert(_collections.containsKey(collectionName), 'The Box "$collectionName" does not exists.');

await _collections[collectionName]!.delete(key);
Box box = await _collections[collectionName]!;
await box.delete(key);
}

@override
Future<void> deleteMany({String collectionName = '', List<String> keys = const []}) async {
assert(_collections.containsKey(collectionName), 'The Box "$collectionName" does not exists.');

Box box = await _collections[collectionName]!;
if (keys.isNotEmpty) {
_collections[collectionName]!.deleteAll(keys);
await box.deleteAll(keys);
}
}

@override
Future<void> deleteAll({String collectionName = ''}) async {
assert(_collections.containsKey(collectionName), 'The Box "$collectionName" does not exists.');

await _collections[collectionName]!.clear();
Box box = await _collections[collectionName]!;
await box.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'base_service.dart';
/// A placeholder storage class when [LocalStorageType.none] is selected in env.dart.
class NoOpStorage implements LocalStorageService {
@override
Future<void> add(String name) async {}
void add(String name) {}

@override
Future<void> create({
Expand Down
2 changes: 1 addition & 1 deletion lib/vaahextendflutter/services/storage/local/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ abstract class LocalStorage {
/// LocalStorage.add('posts');
/// //used only with Hive
/// ```
Future<void> add(String collectionName) {
void add(String collectionName) {
return _instanceLocal.add(collectionName);
}

Expand Down

0 comments on commit 3e72079

Please sign in to comment.