Skip to content

Commit 5bc1be7

Browse files
committed
✨ ensure box is open before performing operations
1 parent 434a894 commit 5bc1be7

File tree

1 file changed

+53
-12
lines changed

1 file changed

+53
-12
lines changed

packages/hyper_storage_hive/lib/src/backend.dart

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ class HiveBackend extends StorageBackend {
4545
@override
4646
Future<void> init() async => _box ??= await Hive.openBox(name);
4747

48+
Future<void> _ensureBoxOpen() async {
49+
if (_box case Box(isOpen: false)) _box = await Hive.openBox(name);
50+
}
51+
4852
@override
4953
Future<HyperStorageContainer> container(String name, {String? delimiter}) async {
5054
final backend = HiveBackend(boxName: name);
@@ -53,47 +57,84 @@ class HiveBackend extends StorageBackend {
5357
}
5458

5559
@override
56-
Future<void> setString(String key, String value) => box.put(key, value);
60+
Future<void> setString(String key, String value) async {
61+
await _ensureBoxOpen();
62+
return box.put(key, value);
63+
}
5764

5865
@override
59-
Future<String?> getString(String key) async => box.get(key);
66+
Future<String?> getString(String key) async {
67+
await _ensureBoxOpen();
68+
return box.get(key);
69+
}
6070

6171
@override
62-
Future<void> setBool(String key, bool value) => box.put(key, value);
72+
Future<void> setBool(String key, bool value) async {
73+
await _ensureBoxOpen();
74+
return box.put(key, value);
75+
}
6376

6477
@override
65-
Future<bool?> getBool(String key) async => box.get(key);
78+
Future<bool?> getBool(String key) async {
79+
await _ensureBoxOpen();
80+
return box.get(key);
81+
}
6682

6783
@override
68-
Future<void> setDouble(String key, double value) => box.put(key, value);
84+
Future<void> setDouble(String key, double value) async {
85+
await _ensureBoxOpen();
86+
return box.put(key, value);
87+
}
6988

7089
@override
71-
Future<double?> getDouble(String key) async => box.get(key);
90+
Future<double?> getDouble(String key) async {
91+
await _ensureBoxOpen();
92+
return box.get(key);
93+
}
7294

7395
@override
74-
Future<void> setInt(String key, int value) => box.put(key, value);
96+
Future<void> setInt(String key, int value) async {
97+
await _ensureBoxOpen();
98+
return box.put(key, value);
99+
}
75100

76101
@override
77-
Future<int?> getInt(String key) async => await box.get(key);
102+
Future<int?> getInt(String key) async {
103+
await _ensureBoxOpen();
104+
return box.get(key);
105+
}
78106

79107
@override
80108
Future<Map<String, dynamic>> getAll(Set<String> allowList) async {
109+
await _ensureBoxOpen();
81110
final data = <String, dynamic>{...box.toMap()};
82111
if (allowList.isEmpty) return {};
83112
return data..removeWhere((key, value) => !allowList.contains(key));
84113
}
85114

86115
@override
87-
Future<Set<String>> getKeys() async => box.keys.map((key) => key.toString()).toSet();
116+
Future<Set<String>> getKeys() async {
117+
await _ensureBoxOpen();
118+
return box.keys.map((key) => key.toString()).toSet();
119+
}
88120

89121
@override
90-
Future<void> remove(String key) => box.delete(key);
122+
Future<void> remove(String key) async {
123+
await _ensureBoxOpen();
124+
return box.delete(key);
125+
}
91126

92127
@override
93-
Future<void> removeAll(Iterable<String> keys) => box.deleteAll(keys);
128+
Future<void> removeAll(Iterable<String> keys) async {
129+
await _ensureBoxOpen();
130+
return box.deleteAll(keys);
131+
}
94132

95133
@override
96-
Future<bool> containsKey(String key) async => box.containsKey(key);
134+
Future<bool> containsKey(String key) async {
135+
await _ensureBoxOpen();
136+
return box.containsKey(key);
137+
}
97138

98139
@override
99140
Future<void> clear() => Hive.deleteFromDisk();

0 commit comments

Comments
 (0)