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 page and changed it slightly to confirm that compaction is surely triggered at the fourth execution as the condition specified as deletedEntries > 3.
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
Thanks for reporting. This is indeed not supposed to happen.
Have similar problem
Still facing issue when you delete a box