requestValueChange method

Future<void> requestValueChange(
  1. String key,
  2. String serializedValue,
  3. String kind,
  4. bool legacy,
)

Requests the value change for the given key and posts an empty event when finished.

Implementation

Future<void> requestValueChange(
  String key,
  String serializedValue,
  String kind,
  bool legacy,
) async {
  final Object? value = jsonDecode(serializedValue);
  if (legacy) {
    final EncryptedSharedPreferences legacyPrefs =
        EncryptedSharedPreferences.getInstance();
    // we need to check the kind because sometimes a double
    // gets interpreted as an int. If this was not an issue
    // we'd only need to do a simple pattern matching on value.
    switch (kind) {
      case 'int':
        await legacyPrefs.setInt(key, value! as int);
        break;
      case 'bool':
        await legacyPrefs.setBool(key, value! as bool);
        break;
      case 'double':
        await legacyPrefs.setDouble(key, value! as double);
        break;
      case 'String':
        await legacyPrefs.setString(key, value! as String);
        break;
      case 'List<String>':
        await legacyPrefs.setStringList(
          key,
          (value! as List<Object?>).cast(),
        );
    }
  } else {
    final EncryptedSharedPreferencesAsync prefs =
        EncryptedSharedPreferencesAsync.getInstance();
    // we need to check the kind because sometimes a double
    // gets interpreted as an int. If this was not an issue
    // we'd only need to do a simple pattern matching on value.
    switch (kind) {
      case 'int':
        await prefs.setInt(key, value! as int);
        break;
      case 'bool':
        await prefs.setBool(key, value! as bool);
        break;
      case 'double':
        await prefs.setDouble(key, value! as double);
        break;
      case 'String':
        await prefs.setString(key, value! as String);
        break;
      case 'List<String>':
        await prefs.setStringList(
          key,
          (value! as List<Object?>).cast(),
        );
    }
  }
  _postEvent('${_eventPrefix}change_value', <String, Object?>{});
}