init method

  1. @override
Future<StorageRepository> init()
override

Initializes the storage repository.

This method should be called immediately after creating an instance of this class. It attempts to open the Hive box, and in case of failure, it resets the storage and retries.

Returns an instance of StorageRepository once initialized.

Implementation

@override
Future<StorageRepository> init() async {
  try {
    // Attempt to open the Hive storage box.
    storage = await Hive.openBox(key);
  } catch (e) {
    debugPrint(e.toString());

    // If an error occurs, delete the storage box and retry opening it.
    Hive.deleteBoxFromDisk(key);
    try {
      storage = await Hive.openBox(key);
    } catch (e) {
      debugPrint(e.toString());
    }
  }
  return this;
}