'Box has already been closed' error right after openBox()
- Dominant language
- Dart
- Stars
- 4.4k
- Forks
- 449
- PR merge metrics
- No merged PRs in 30d
Description
**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()`. Using `await` for `box.put()` and `box.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](https://docs.hivedb.dev/#/README?id=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`.
```dart
import 'dart:io';
import 'package:hive/hive.dart';
void main() async {
Hive.init(Directory.current.path);
final box = await Hive.openBox(
'testBox',
compactionStrategy: (entries, deletedEntries) => deletedEntries > 3,
);
box.put('name', 'David');
box.delete('name');
print('Name: ${box.get('name')}');
box.close();
}
```
**Version**
- Platform: Windows
- Flutter version: N/A; only Dart without Flutter
- Hive version: 1.3.0
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.