saveData<T> method Null safety

Future<bool> saveData<T>(
  1. String key,
  2. T value
)

Save data with primary data types: String, List

Implementation

Future<bool> saveData<T>(String key, T value) async {
  // Get SharedPreferences ref
  final SharedPreferences ref = await preferences;

  // Delete if value is null
  if (value == null) {
    return ref.remove(key);
  }

  // With primary data type, save to local
  if (value is String) {
    return ref.setString(key, value);
  } else if (value is bool) {
    return ref.setBool(key, value);
  } else if (value is int) {
    return ref.setInt(key, value);
  } else if (value is double) {
    return ref.setDouble(key, value);
  } else if (value is List<String>) {
    return ref.setStringList(key, value);
  }
  // Default return false
  return false;
}