Skip to content
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

Open
kaboc opened this issue Jan 30, 2020 · 18 comments
Open

'Box has already been closed' error right after openBox() #207

kaboc opened this issue Jan 30, 2020 · 18 comments
Assignees
Labels
bug Something isn't working

Comments

@kaboc
Copy link

kaboc commented Jan 30, 2020

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
@kaboc kaboc added the problem An unconfirmed bug. label Jan 30, 2020
@simc
Copy link
Member

simc commented Jan 30, 2020

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

@simc simc added bug Something isn't working and removed problem An unconfirmed bug. labels Jan 30, 2020
@incker
Copy link

incker commented Feb 12, 2020

Have similar problem

@ch-muhammad-adil
Copy link

Still facing issue when you delete a box

@LiveRock
Copy link

Is the issue resolved?

@pedrolemoz
Copy link

pedrolemoz commented Jul 24, 2021

Facing the same issue while testing my code. This error occurs when I run all tests instead of a single test.

When running the test individually:

image

When I run all tests:

image

@pedrolemoz
Copy link

I solved this issue by getting another instance of the current box, and then closing it before the act

image

@flexagoon
Copy link

I'm having the same issue

@bazzscript
Copy link

am also still having this issue

2 similar comments
@bazzscript
Copy link

am also still having this issue

@michalsmolarek
Copy link

am also still having this issue

@anilthapa1939
Copy link

same error

@alidaoud
Copy link

any solutions ? I am having the same issue 😑

@Tom3652
Copy link

Tom3652 commented Mar 2, 2022

Same for me using the latest version : hive: ^2.0.5

@fuadreza
Copy link

Mine happen because I use the same box again.
First I run get then if empty result I run put.

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

@fuadreza
Copy link

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.

@fuadreza
Copy link

I also have another workaround, so basically I just add await to box.close();. Because box.close() is running asynchronously that there is a chance that you may be open the box when box is about to close. So when the box is opened, then the box closed after a while. Correct me if I'm wrong.

@dealluer
Copy link

Similar issue here. It seems that deleteFromDisk() does not wait for the actual deletion of the box?

@afzl-wtu
Copy link

afzl-wtu commented Apr 2, 2023

Similar Issue on Windows app with latest FlutterHive version: hive_flutter: ^1.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests