saveValue<T> static method

Future<void> saveValue<T>(
  1. String key,
  2. T value
)

Saves a value of generic type T to shared preferences.

Implementation

static Future<void> saveValue<T>(String key, T value) async {
  final SharedPreferences prefs = await SharedPreferences.getInstance();

  if (value is String) {
    await prefs.setString(key, value);
  } else if (value is int) {
    await prefs.setInt(key, value);
  } else if (value is bool) {
    await prefs.setBool(key, value);
  } else if (value is double) {
    await prefs.setDouble(key, value);
  } else if (value is List<String>) {
    await prefs.setStringList(key, value);
  } else {
    throw Exception('Unsupported type');
  }
}