save<T> method
Save a setting to the Theme service, using key
as its storage key.
Implementation
@override
Future<void> save<T>(String key, T value) async {
try {
// Save a nullable bool value.
if (sameTypes<T, bool?>()) {
if (value == null) {
await _prefs.setInt(key, -1);
if (_debug) {
debugPrint(
'SharedPrefs saved type bool? as int: $key NULL as $value');
}
return;
} else {
await _prefs.setInt(key, (value as bool) ? 1 : 0);
if (_debug) {
debugPrint('SharedPrefs saved type bool? int: $key as $value');
}
return;
}
}
// Save a none nullable bool value.
if (sameTypes<T, bool>()) {
await _prefs.setInt(key, (value as bool) ? 1 : 0);
if (_debug) {
debugPrint('SharedPrefs saved type bool as int : $key as $value');
}
return;
}
// Save a nullable int value.
if (sameTypes<T, int?>()) {
if (value == null) {
await _prefs.setInt(key, -1);
if (_debug) {
debugPrint('SharedPrefs saved type int? : $key NULL as -1');
}
return;
} else {
await _prefs.setInt(key, value as int);
if (_debug) {
debugPrint('SharedPrefs saved type int? : $key as $value');
}
return;
}
}
// Save a none nullable int value.
if (sameTypes<T, int>()) {
await _prefs.setInt(key, value as int);
if (_debug) {
debugPrint('SharedPrefs saved type int : $key as $value');
}
return;
}
// Save a nullable double value.
if (sameTypes<T, double?>()) {
if (value == null) {
await _prefs.setDouble(key, -1.0);
if (_debug) {
debugPrint('SharedPrefs saved type double? : $key NULL as -1.0');
}
return;
} else {
await _prefs.setDouble(key, value as double);
if (_debug) {
debugPrint('SharedPrefs saved type double? : $key as $value');
}
return;
}
}
// Save a none nullable double value.
if (sameTypes<T, double>()) {
await _prefs.setDouble(key, value as double);
if (_debug) {
debugPrint('SharedPrefs saved type double : $key as $value');
}
return;
}
// Save a nullable String value.
if (sameTypes<T, String?>()) {
if (value == null) {
await _prefs.setString(key, '<NULL>');
if (_debug) {
debugPrint('SharedPrefs saved type String? : $key NULL as <NULL>');
}
return;
} else {
await _prefs.setString(key, value as String);
if (_debug) {
debugPrint('SharedPrefs saved type String? : $key as $value');
}
return;
}
}
// Save a none nullable String value.
if (sameTypes<T, String>()) {
await _prefs.setString(key, value as String);
if (_debug) {
debugPrint('SharedPrefs saved type String : $key as $value');
}
return;
}
// Save a nullable Color value.
if (sameTypes<T, Color?>()) {
if (value == null) {
await _prefs.setInt(key, -1);
if (_debug) {
debugPrint('SharedPrefs saved type Color? : $key NULL as -1');
}
return;
} else {
await _prefs.setInt(key, (value as Color).value);
if (_debug) {
debugPrint(
'SharedPrefs saved type Color? : $key as ${value.value}');
}
return;
}
}
// Save a none nullable Color value.
if (sameTypes<T, Color>()) {
await _prefs.setInt(key, (value as Color).value);
if (_debug) {
debugPrint(
'SharedPrefs saved type Color : $key as ${value.value}');
}
return;
}
// Store Enums as their int index value. This may break if enum
// definitions are changed in any other way than adding more enums to its
// end. Changing the order of enum value in an enum may break the enum
// order. The load handles removal of enum value by returning retrieved
// out of bounds as the provided default enum value.
//
// Save a none nullable Enum value.
if (value is Enum) {
await _prefs.setInt(key, value.index);
if (_debug) {
debugPrint(
'SharedPrefs saved type Enum : $key as ${value.index}');
}
return;
}
// Save a nullable Enum value - note any NULL value of ANY type will
// match here with this comparison. However, if we are not storing any
// other types than the above atomic types handled here, then this last
// case will be a nullable Enum.
// So this should work so we do not have to check for every specific Enum
// that we support for loading, that have to have their own actual Enum
// type based back conversion.
if (value is Enum?) {
if (value == null) {
await _prefs.setInt(key, -1);
if (_debug) {
debugPrint('SharedPrefs saved type Enum? : $key NULL as -1');
}
return;
} else {
// This type check is not unnecessary, because value is generic
// ignore: unnecessary_type_check
if (value is Enum) {
await _prefs.setInt(key, value.index);
if (_debug) {
debugPrint(
'SharedPrefs saved type Enum? : $key as ${value.index}');
}
return;
}
}
}
} catch (e) {
debugPrint('SharedPrefs save ERROR');
debugPrint(' Error message ...... : $e');
debugPrint(' Store key .......... : $key');
debugPrint(' Save value ......... : $value');
}
}