hive icon indicating copy to clipboard operation
hive copied to clipboard

HiveError: Box has already been closed.

Open bilalgodesto opened this issue 2 years ago • 3 comments

Steps to Reproduce I am using hivebox to store connections. Each key is separate connection. Connections are added/updated frequently I am using below code to make sure box is open appropriately but i am still getting HiveError: Box has already been closed. error when i try to access a key in a box

Code sample

static Future<Box<dynamic>> openIt() async {
    var connectionBox = await Hive.openBox(hiveBox);
    if (Hive.isBoxOpen(hiveBox) == false) {
      await Hive.openBox(hiveBox);
     
    } else {
      await connectionBox.close();
      connectionBox = await Hive.openBox(hiveBox);
      
    }

    return connectionBox;
  } 

static Future<Map<String, String>> getUnReadCount(String uid) async {
    var connectionBox = await openIt();
    var prevData = connectionBox.get(uid);
    Map<String, String> returnMap = {};
    try {    
      returnMap['unreadCount'] = '0';
      return returnMap;
    } catch (e) {
      //this exception is throwing the error
    }


  }

Version

  • Platform: iOS
  • Flutter version: 2.2.1
  • Hive version: hive: ^2.0.4, hive_flutter: ^1.0.0

bilalgodesto avatar Sep 06 '21 16:09 bilalgodesto

@bilalgodesto

You don't need to define openIt() method and you should open box from main function, as openBox is a future you should use .then() before running runApp, if you many boxes in hive you can wrap your MaterialApp with FutureBuilder, after that you don't need to open any box and you can use Hive.box instead

for example:

Future<void> initHiveDriver() async {
  final appDocumentDirectory = await path_provider.getApplicationDocumentsDirectory();
  final hiveDb = Directory('${appDocumentDirectory.path}/database');
  //hiveDb.delete(recursive: true);
  await Hive.initFlutter(hiveDb.path);
  //await Hive.initFlutter(appDocumentDirectory.path);

  Hive.registerAdapter(UserAdapter());
  final u = await Hive.openBox<User>('user');
  //await u.clear();
}


initHiveDriver().then((value) {
  SystemChrome.setPreferredOrientations(
    [
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ],
  ).then((val) {
    runApp(
      ProviderScope(observers: [Logger()], child: MyApp()),
    );
  });
});

pishguy avatar Sep 07 '21 12:09 pishguy

I understand your point and i will try it but when we receive notification from firebase and app is in background we want to log that notification in hiveBox then as background method runs on different isolate Box opening in main functions are ignored. thats why we need the openIt function.

bilalgodesto avatar Sep 07 '21 12:09 bilalgodesto

Have similar problem

LuisGustav0 avatar Jul 09 '22 20:07 LuisGustav0

I think it's a bug since I've been trying all day fixing it and ending up giving up.

has any one find anything yet ..?????

basharalbashier avatar Dec 02 '22 23:12 basharalbashier

Same Issue on windows : hive_flutter: ^1.1.0

afzl-wtu avatar Apr 02 '23 08:04 afzl-wtu