init static method

Future<void> init({
  1. required StorageConfig config,
})

Implementation

static Future<void> init({
  required StorageConfig config,
}) async {
  _config = config;
  final storage = instance;

  if (_config.type == StorageType.sharedPreferences) {
    storage._prefs = await SharedPreferences.getInstance();
  } else {
    if (_config.enableEncryption) {
      final deviceId =
          _config.deviceId ?? await DeviceIdentifier.getDeviceId();
      final userId = _config.userId ?? 'default_user';

      storage._cryptoAdapter = HiveCryptoAdapter(
        userId: userId,
        deviceId: deviceId,
      );

      storage._hiveBox = await Hive.openBox(
        _config.hiveBoxName!,
        encryptionCipher:
            HiveAesCipher(storage._cryptoAdapter!.getHiveEncryptionKey()),
      );
    } else {
      storage._hiveBox = await Hive.openBox(_config.hiveBoxName!);
    }
  }

  if (_config.enableEncryption &&
      _config.type == StorageType.sharedPreferences) {
    final deviceId = _config.deviceId ?? await DeviceIdentifier.getDeviceId();
    final userId = _config.userId ?? 'default_user';

    storage._encryptionKey = CryptoUtils.generateUserKey(
      userId: userId,
      deviceId: deviceId,
    );
  }
}