openBox<B> method

Future<Box<B>> openBox<B>(
  1. String name, {
  2. bool retry = true,
})

Implementation

Future<Box<B>> openBox<B>(String name, {bool retry = true}) async {
  // start using snake_case name only if box
  // does not exist in order not to break present boxes
  if (!await hive.boxExists(name)) {
    // since the snakeCase function strips leading _'s
    // we capture them restore them afterwards
    final matches = RegExp(r'^(_+)[a-z]').allMatches(name);
    name = ReCase(name).snakeCase;
    if (matches.isNotEmpty) {
      name = matches.first.group(1)! + name;
    }
  }
  _boxes.add(name);
  try {
    return await hive.openBox<B>(name, encryptionCipher: encryptionCipher);
  } catch (_) {
    if (clear == LocalStorageClearStrategy.whenError && retry) {
      // if box is corrupted, remove and open a new one (retry only once)
      await deleteBox(name);
      return await openBox(name, retry: false);
    } else {
      rethrow;
    }
  }
}