Hive: 'Box has already been closed' error right after openBox()

Created on 31 Jan 2020  路  3Comments  路  Source: hivedb/hive

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

  • Platform: Windows
  • Flutter version: N/A; only Dart without Flutter
  • Hive version: 1.3.0
bug

All 3 comments

Thanks for reporting. This is indeed not supposed to happen.

Have similar problem

Still facing issue when you delete a box

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yaymalaga picture yaymalaga  路  4Comments

ninest picture ninest  路  3Comments

joaovbalmeida picture joaovbalmeida  路  4Comments

yannickvg picture yannickvg  路  4Comments

azilvl picture azilvl  路  3Comments