New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
'Box has already been closed' error right after openBox() #207
Comments
Thanks for reporting. This is indeed not supposed to happen. |
Have similar problem |
Still facing issue when you delete a box |
Is the issue resolved? |
I'm having the same issue |
am also still having this issue |
2 similar comments
am also still having this issue |
am also still having this issue |
same error |
any solutions ? I am having the same issue 😑 |
Same for me using the latest version : hive: ^2.0.5 |
Mine happen because I use the same box again. Here's my code. put({
required String boxName,
required String key,
required String value,
}) async {
try {
final Box box = await Hive.openBox(boxName);
await box.put(key, value);
if(box.isOpen) {
box.close();
}
} catch (error) {
throw HiveError('Hive error: ${error.toString()}');
}
}
Future<String> get({
required String boxName,
required String key,
}) async {
try {
final Box box = await Hive.openBox(boxName);
String? result = await box.get(key);
if(box.isOpen) {
box.close();
}
if(result != null) {
return result;
} else {
throw HiveError('Result Empty');
}
} catch (error) {
throw HiveError('Hive error: ${error.toString()}');
}
} I'm using hive: ^2.0.6 |
My temporary solution is to open the box again when it is not opened yet. put({
required String boxName,
required String key,
required String value,
}) async {
try {
Box box = await Hive.openBox(boxName);
if(!box.isOpen) {
box = await Hive.openBox(boxName);
}
if(box.isOpen) {
await box.put(key, value);
box.close();
}
} catch (error) {
throw HiveError('Hive error: ${error.toString()}');
}
} I don't really understand what makes that behavior occur. I thought that this behavior is same as BLoC cubit when we emit 2 state after each other that the second state is not emitted. |
I also have another workaround, so basically I just add |
Similar issue here. It seems that deleteFromDisk() does not wait for the actual deletion of the box? |
Similar Issue on Windows app with latest FlutterHive version: hive_flutter: ^1.1.0 |
Steps to Reproduce
Running the code below four times causes an error:
HiveError: Box has already been closed.
It does not occur without
box.close()
. Usingawait
forbox.put()
andbox.delete()
also prevents the error. Even if this is not a bug, the error message may be inaccurate or misleading.Code sample
I used the sample code in the Add to project page and changed it slightly to confirm that compaction is surely triggered at the fourth execution as the condition specified as
deletedEntries > 3
.Version
The text was updated successfully, but these errors were encountered: