openHiveBox method

  1. @override
Future<Box> openHiveBox(
  1. String boxName, {
  2. bool limit = false,
})
override

Implementation

@override
Future<Box> openHiveBox(String boxName, {bool limit = false}) async {
  AppConfig.logger.t('openHiveBox $boxName');
  final box = await Hive.openBox(boxName).onError((error, stackTrace) async {
    AppConfig.logger.e('Failed to open $boxName Box');
    final Directory dir = await getApplicationDocumentsDirectory();
    final String dirPath = dir.path;
    final File dbFile = File('$dirPath/$boxName.hive');
    final File lockFile = File('$dirPath/$boxName.lock');

    await dbFile.delete();
    await lockFile.delete();
    await Hive.openBox(boxName);
    throw 'Failed to open $boxName Box\nError: $error';
  });

  if (limit && box.length > 500) {
    AppConfig.logger.w("Box $boxName would be cleared as it exceeded the limit");
    box.clear();
  }

  return box;
}