From 3e7207992eaf1521c837a6d3e4061af29360f7db Mon Sep 17 00:00:00 2001 From: Mohd Imran Date: Fri, 14 Jun 2024 16:44:21 +0530 Subject: [PATCH] Fixed: async gap error --- .../storage/local/services/base_service.dart | 2 +- .../services/flutter_secure_storage.dart | 2 +- .../services/storage/local/services/hive.dart | 45 ++++++++++++------- .../storage/local/services/no_op_storage.dart | 2 +- .../services/storage/local/storage.dart | 2 +- 5 files changed, 32 insertions(+), 21 deletions(-) diff --git a/lib/vaahextendflutter/services/storage/local/services/base_service.dart b/lib/vaahextendflutter/services/storage/local/services/base_service.dart index 45e97ca..1f9267b 100644 --- a/lib/vaahextendflutter/services/storage/local/services/base_service.dart +++ b/lib/vaahextendflutter/services/storage/local/services/base_service.dart @@ -1,5 +1,5 @@ abstract class LocalStorageService { - Future add(String collectionName); + void add(String collectionName); Future create({String collectionName, required String key, required String value}); diff --git a/lib/vaahextendflutter/services/storage/local/services/flutter_secure_storage.dart b/lib/vaahextendflutter/services/storage/local/services/flutter_secure_storage.dart index 87119b1..1f4888a 100644 --- a/lib/vaahextendflutter/services/storage/local/services/flutter_secure_storage.dart +++ b/lib/vaahextendflutter/services/storage/local/services/flutter_secure_storage.dart @@ -8,7 +8,7 @@ class LocalStorageWithFlutterSecureStorage implements LocalStorageService { final _storage = const FlutterSecureStorage(); @override - Future add(String name) async {} + void add(String name) {} @override Future create({ diff --git a/lib/vaahextendflutter/services/storage/local/services/hive.dart b/lib/vaahextendflutter/services/storage/local/services/hive.dart index 6a2fa85..042725c 100644 --- a/lib/vaahextendflutter/services/storage/local/services/hive.dart +++ b/lib/vaahextendflutter/services/storage/local/services/hive.dart @@ -4,13 +4,13 @@ import 'base_service.dart'; /// A class implementing LocalStorageService interface using Hive as storage backend. class LocalStorageWithHive implements LocalStorageService { - final Map _collections = {}; + final Map> _collections = {}; @override - Future 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 @@ -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 @@ -36,7 +38,8 @@ class LocalStorageWithHive implements LocalStorageService { Future 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; } @@ -45,8 +48,6 @@ class LocalStorageWithHive implements LocalStorageService { String collectionName = '', required List keys, }) async { - assert(_collections.containsKey(collectionName), 'The Box "$collectionName" does not exists.'); - if (keys.isNotEmpty) { Map result = {}; for (String k in keys) { @@ -62,9 +63,13 @@ class LocalStorageWithHive implements LocalStorageService { Future> readAll({String collectionName = ''}) async { assert(_collections.containsKey(collectionName), 'The Box "$collectionName" does not exists.'); - Map result = _collections[collectionName]! - .toMap() - .map((key, value) => MapEntry(key.toString(), value?.toString())); + Box box = await _collections[collectionName]!; + Map result = box.toMap().map( + (key, value) => MapEntry( + key.toString(), + value?.toString(), + ), + ); return result; } @@ -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 @@ -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 @@ -112,15 +120,17 @@ class LocalStorageWithHive implements LocalStorageService { Future 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 deleteMany({String collectionName = '', List 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); } } @@ -128,6 +138,7 @@ class LocalStorageWithHive implements LocalStorageService { Future 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(); } } diff --git a/lib/vaahextendflutter/services/storage/local/services/no_op_storage.dart b/lib/vaahextendflutter/services/storage/local/services/no_op_storage.dart index bc28c95..1e4e3e6 100644 --- a/lib/vaahextendflutter/services/storage/local/services/no_op_storage.dart +++ b/lib/vaahextendflutter/services/storage/local/services/no_op_storage.dart @@ -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 add(String name) async {} + void add(String name) {} @override Future create({ diff --git a/lib/vaahextendflutter/services/storage/local/storage.dart b/lib/vaahextendflutter/services/storage/local/storage.dart index ce20535..ad9e19c 100644 --- a/lib/vaahextendflutter/services/storage/local/storage.dart +++ b/lib/vaahextendflutter/services/storage/local/storage.dart @@ -31,7 +31,7 @@ abstract class LocalStorage { /// LocalStorage.add('posts'); /// //used only with Hive /// ``` - Future add(String collectionName) { + void add(String collectionName) { return _instanceLocal.add(collectionName); }