load<T> method

  1. @override
Future<T> load<T>(
  1. String key,
  2. T defaultValue
)
override

Loads a setting from the Theme service, stored with key string.

Implementation

@override
Future<T> load<T>(String key, T defaultValue) async {
  try {
    if (_debug) {
      debugPrint('SharedPrefs has type .......... : $key '
          'as ${defaultValue.runtimeType}');
    }
    // T is boolean nullable value.
    if (sameTypes<T, bool?>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded type bool?   : $key as $value');
        if (value == null) {
          debugPrint('      Returned as default value : $defaultValue');
        }
      }
      // If value is null, we had no key for it, we should return the
      // default value, that may also be null, but it might not be too,
      // but it will be of nullable bool type.
      if (value == null) return defaultValue;
      bool? result;
      if (value == 0) result = false;
      if (value >= 1) result = true;
      return result as T; // Returns null if value was less than zero
    }
    // T is boolean none nullable value.
    if (sameTypes<T, bool>()) {
      // For a non nullable, we can just use the defaultValue as fallback if
      // a key did not exist.
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded type bool    : $key as $value');
        if (value == null) {
          debugPrint('      Returned as default value : $defaultValue');
        }
      }
      // If value is null, we had no key for it, we should return the
      // default value, that always has a value that is never null.
      if (value == null) {
        return defaultValue;
      } else {
        bool result;
        if (value == 0) {
          result = false;
        } else {
          result = true;
        }
        return result as T;
      }
    }
    // T is integer nullable value.
    if (sameTypes<T, int?>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded type int?    : $key as $value');
      }
      // If value is null, we had no key for it, we should return the
      // default value, that may also be null, but it might not be too,
      // but it will be of nullable int type.
      if (value == null) return defaultValue;
      // If it was not null, but was less than zero, that we use to represent
      // stored null, we will return null of type T.
      if (value < 0) return null as T;
      // else we return the value.
      return value as T;
    }
    // T is integer none nullable value.
    if (sameTypes<T, int>()) {
      // For a non nullable, we can just use the defaultValue as fallback if
      // a key did not exist.
      final int value = _prefs.getInt(key) ?? defaultValue as int;
      if (_debug) {
        debugPrint('SharedPrefs loaded type int     : $key as $value');
      }
      // else we return the value.
      return value as T;
    }
    // T is double nullable value.
    if (sameTypes<T, double?>()) {
      final double? value = _prefs.getDouble(key) ?? defaultValue as double?;
      if (_debug) {
        debugPrint('SharedPrefs loaded type double? : $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0.0) return null as T;
      return value as T;
    }
    // T is double none nullable value.
    if (sameTypes<T, double>()) {
      final double value = _prefs.getDouble(key) ?? defaultValue as double;
      if (_debug) {
        debugPrint('SharedPrefs loaded type double  : $key as $value');
      }
      return value as T;
    }
    // T is String nullable value.
    if (sameTypes<T, String?>()) {
      final String? value = _prefs.getString(key) ?? defaultValue as String?;
      if (_debug) {
        debugPrint('SharedPrefs loaded type String? : $key as $value');
      }
      if (value == null) return defaultValue;
      if (value == '<NULL>') return null as T;
      return value as T;
    }
    // T is String none nullable value.
    if (sameTypes<T, String>()) {
      final String value = _prefs.getString(key) ?? defaultValue as String;
      if (_debug) {
        debugPrint('SharedPrefs loaded type String  : $key as $value');
      }
      return value as T;
    }
    // T is Color nullable value.
    if (sameTypes<T, Color?>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded Color?       : $key as $value');
      }
      // If value is null, we had no key for it, we should return the
      // default value, that may also be null, but it might not be too,
      // but it will be of nullable Color type.
      if (value == null) return defaultValue;
      // Return negative out of bounds Color value as null as T;
      if (value < 0x00000000) return null as T;
      // Return positive out of bounds Color value as default value;
      if (value > 0xFFFFFFFF) return defaultValue;
      // else we return the value as a Color of type T.
      return Color(value) as T;
    }
    // T is Color none nullable value.
    if (sameTypes<T, Color>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded Color        : $key as $value');
      }
      // If value is null, we had no key for it, we should return the
      // default value, that is a none nullable Color.
      if (value == null) return defaultValue;
      // Return negative out of bounds Color value as null as T;
      if (value < 0x00000000) return null as T;
      // Return positive out of bounds Color value as default value;
      if (value > 0xFFFFFFFF) return defaultValue;
      // else we return the value as a Color of type T.
      return Color(value) as T;
    }
    // We have to explicitly handle each Enum type we have stored to be able
    // to convert it back to its type, which we have in its type parameter.
    //
    // T is ThemeMode nullable value.
    if (sameTypes<T, ThemeMode?>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded ThemeMode?   : $key as $value');
      }
      // If value is null, we had no key for it, we should return the
      // default value, that may also be null, but it might not be too,
      // but it will be of nullable ThemeMode type.
      if (value == null) return defaultValue;
      // Return negative out of Enum bounds value as null;
      if (value < 0) return null as T;
      // Return positive out of bounds index value as default value;
      if (value >= ThemeMode.values.length) return defaultValue;
      // else we return the value as a ThemeMode of type T.
      return ThemeMode.values[value] as T;
    }
    // T is ThemeMode none nullable value.
    if (sameTypes<T, ThemeMode>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded ThemeMode    : $key as $value');
      }
      // If value is null, we had no key for it, we should return the
      // default value, that may also be null, but it might not be too,
      // but it will be of nullable ThemeMode type.
      if (value == null) return defaultValue;
      // Return negative out of Enum bounds value as default value;
      if (value < 0) return defaultValue;
      // Return positive out of bounds index value as default value;
      if (value >= ThemeMode.values.length) return defaultValue;
      // else we return the value as a ThemeMode of type T.
      return ThemeMode.values[value] as T;
    }
    //
    // The rest of the supported Enum types are a repeat of the above pattern.
    //
    // T is FlexScheme nullable value.
    if (sameTypes<T, FlexScheme?>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded FlexScheme?  : $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return null as T;
      if (value >= FlexScheme.values.length) return defaultValue;
      return FlexScheme.values[value] as T;
    }
    // T is FlexScheme none nullable value.
    if (sameTypes<T, FlexScheme>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded FlexScheme   : $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return defaultValue;
      if (value >= FlexScheme.values.length) return defaultValue;
      return FlexScheme.values[value] as T;
    }
    // T is FlexSurfaceMode nullable value.
    if (sameTypes<T, FlexSurfaceMode?>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded FlexSurfaceMode? : $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return null as T;
      if (value >= FlexSurfaceMode.values.length) return defaultValue;
      return FlexSurfaceMode.values[value] as T;
    }
    // T is FlexSurfaceMode none nullable value.
    if (sameTypes<T, FlexSurfaceMode>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded FlexSurfaceMode  : $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return defaultValue;
      if (value >= FlexSurfaceMode.values.length) return defaultValue;
      return FlexSurfaceMode.values[value] as T;
    }
    // T is FlexInputBorderType nullable value.
    if (sameTypes<T, FlexInputBorderType?>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint(
            'SharedPrefs loaded FlexInputBorderType? : $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return null as T;
      if (value >= FlexInputBorderType.values.length) return defaultValue;
      return FlexInputBorderType.values[value] as T;
    }
    // T is FlexInputBorderType none nullable value.
    if (sameTypes<T, FlexInputBorderType>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint(
            'SharedPrefs loaded FlexInputBorderType  : $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return defaultValue;
      if (value >= FlexInputBorderType.values.length) return defaultValue;
      return FlexInputBorderType.values[value] as T;
    }
    // T is FlexAppBarStyle nullable value.
    if (sameTypes<T, FlexAppBarStyle?>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded FlexAppBarStyle? : $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return null as T;
      if (value >= FlexAppBarStyle.values.length) return defaultValue;
      return FlexAppBarStyle.values[value] as T;
    }
    // T is FlexAppBarStyle none nullable value.
    if (sameTypes<T, FlexAppBarStyle>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded FlexAppBarStyle  : $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return defaultValue;
      if (value >= FlexAppBarStyle.values.length) return defaultValue;
      return FlexAppBarStyle.values[value] as T;
    }
    // T is FlexTabBarStyle nullable value.
    if (sameTypes<T, FlexTabBarStyle?>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded FlexTabBarStyle? : $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return null as T;
      if (value >= FlexTabBarStyle.values.length) return defaultValue;
      return FlexTabBarStyle.values[value] as T;
    }
    // T is FlexTabBarStyle none nullable value.
    if (sameTypes<T, FlexTabBarStyle>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded FlexTabBarStyle  : $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return defaultValue;
      if (value >= FlexTabBarStyle.values.length) return defaultValue;
      return FlexTabBarStyle.values[value] as T;
    }
    // T is FlexSystemNavBarStyle nullable value.
    if (sameTypes<T, FlexSystemNavBarStyle?>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint(
            'SharedPrefs loaded FlexSystemNavBarStyle? : $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return null as T;
      if (value >= FlexSystemNavBarStyle.values.length) return defaultValue;
      return FlexSystemNavBarStyle.values[value] as T;
    }
    // T is FlexSystemNavBarStyle none nullable value.
    if (sameTypes<T, FlexSystemNavBarStyle>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint(
            'SharedPrefs loaded FlexSystemNavBarStyle  : $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return defaultValue;
      if (value >= FlexSystemNavBarStyle.values.length) return defaultValue;
      return FlexSystemNavBarStyle.values[value] as T;
    }
    // T is SchemeColor nullable value.
    if (sameTypes<T, SchemeColor?>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded SchemeColor? : $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return null as T;
      if (value >= SchemeColor.values.length) return defaultValue;
      return SchemeColor.values[value] as T;
    }
    // T is SchemeColor none nullable value.
    if (sameTypes<T, SchemeColor>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded SchemeColor  : $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return defaultValue;
      if (value >= SchemeColor.values.length) return defaultValue;
      return SchemeColor.values[value] as T;
    }
    // T is NavigationDestinationLabelBehavior nullable value.
    if (sameTypes<T, NavigationDestinationLabelBehavior?>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded NavigationDestinationLabelBehavior? '
            ': $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return null as T;
      if (value >= NavigationDestinationLabelBehavior.values.length) {
        return defaultValue;
      }
      return NavigationDestinationLabelBehavior.values[value] as T;
    }
    // T is NavigationDestinationLabelBehavior none nullable value.
    if (sameTypes<T, NavigationDestinationLabelBehavior>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded NavigationDestinationLabelBehavior '
            ': $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return defaultValue;
      if (value >= NavigationDestinationLabelBehavior.values.length) {
        return defaultValue;
      }
      return NavigationDestinationLabelBehavior.values[value] as T;
    }
    // T is NavigationRailLabelType nullable value.
    if (sameTypes<T, NavigationRailLabelType?>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint(
            'SharedPrefs loaded NavigationRailLabelType?: $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return null as T;
      if (value >= NavigationRailLabelType.values.length) return defaultValue;
      return NavigationRailLabelType.values[value] as T;
    }
    // T is NavigationRailLabelType none nullable value.
    if (sameTypes<T, NavigationRailLabelType>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint(
            'SharedPrefs loaded NavigationRailLabelType : $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return defaultValue;
      if (value >= NavigationRailLabelType.values.length) return defaultValue;
      return NavigationRailLabelType.values[value] as T;
    }
    // T is FlexSliderIndicatorType nullable value.
    if (sameTypes<T, FlexSliderIndicatorType?>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint(
            'SharedPrefs loaded FlexSliderIndicatorType?: $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return null as T;
      if (value >= FlexSliderIndicatorType.values.length) return defaultValue;
      return FlexSliderIndicatorType.values[value] as T;
    }
    // T is FlexSliderIndicatorType none nullable value.
    if (sameTypes<T, FlexSliderIndicatorType>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint(
            'SharedPrefs loaded FlexSliderIndicatorType : $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return defaultValue;
      if (value >= FlexSliderIndicatorType.values.length) return defaultValue;
      return FlexSliderIndicatorType.values[value] as T;
    }
    // T is ShowValueIndicator nullable value.
    if (sameTypes<T, ShowValueIndicator?>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded ShowValueIndicator?: $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return null as T;
      if (value >= ShowValueIndicator.values.length) return defaultValue;
      return ShowValueIndicator.values[value] as T;
    }
    // T is ShowValueIndicator none nullable value.
    if (sameTypes<T, ShowValueIndicator>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded ShowValueIndicator : $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return defaultValue;
      if (value >= ShowValueIndicator.values.length) return defaultValue;
      return ShowValueIndicator.values[value] as T;
    }
    // T is TabBarIndicatorSize nullable value.
    if (sameTypes<T, TabBarIndicatorSize?>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded TabBarIndicatorSize?: $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return null as T;
      if (value >= TabBarIndicatorSize.values.length) return defaultValue;
      return TabBarIndicatorSize.values[value] as T;
    }
    // T is TabBarIndicatorSize none nullable value.
    if (sameTypes<T, TabBarIndicatorSize>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded TabBarIndicatorSize : $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return defaultValue;
      if (value >= TabBarIndicatorSize.values.length) return defaultValue;
      return TabBarIndicatorSize.values[value] as T;
    }
    // T is AdaptiveTheme is nullable value.
    if (sameTypes<T, AdaptiveTheme?>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded AdaptiveTheme?: $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return null as T;
      if (value >= AdaptiveTheme.values.length) return defaultValue;
      return AdaptiveTheme.values[value] as T;
    }
    // T is AdaptiveTheme none nullable value.
    if (sameTypes<T, AdaptiveTheme>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded AdaptiveTheme : $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return defaultValue;
      if (value >= AdaptiveTheme.values.length) return defaultValue;
      return AdaptiveTheme.values[value] as T;
    }
    // T is FlexPaletteType is nullable value.
    if (sameTypes<T, FlexPaletteType?>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded FlexPaletteType?: $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return null as T;
      if (value >= FlexPaletteType.values.length) return defaultValue;
      return FlexPaletteType.values[value] as T;
    }
    // T is FlexPaletteType none nullable value.
    if (sameTypes<T, FlexPaletteType>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded FlexPaletteType : $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return defaultValue;
      if (value >= FlexPaletteType.values.length) return defaultValue;
      return FlexPaletteType.values[value] as T;
    }
    // T is VisualDensityEnum is nullable value.
    if (sameTypes<T, VisualDensityEnum?>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded VisualDensityEnum?: $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return null as T;
      if (value >= VisualDensityEnum.values.length) return defaultValue;
      return VisualDensityEnum.values[value] as T;
    }
    // T is VisualDensityEnum none nullable value.
    if (sameTypes<T, VisualDensityEnum>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded VisualDensityEnum : $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return defaultValue;
      if (value >= VisualDensityEnum.values.length) return defaultValue;
      return VisualDensityEnum.values[value] as T;
    }
    // T is SplashTypeEnum is nullable value.
    if (sameTypes<T, SplashTypeEnum?>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded SplashTypeEnum?: $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return null as T;
      if (value >= SplashTypeEnum.values.length) return defaultValue;
      return SplashTypeEnum.values[value] as T;
    }
    // T is SplashTypeEnum none nullable value.
    if (sameTypes<T, SplashTypeEnum>()) {
      final int? value = _prefs.getInt(key);
      if (_debug) {
        debugPrint('SharedPrefs loaded SplashTypeEnum : $key as $value');
      }
      if (value == null) return defaultValue;
      if (value < 0) return defaultValue;
      if (value >= SplashTypeEnum.values.length) return defaultValue;
      return SplashTypeEnum.values[value] as T;
    }
  } catch (e) {
    debugPrint('SharedPrefs load ERROR');
    debugPrint(' Error message ...... : $e');
    debugPrint(' Store key .......... : $key');
    debugPrint(' defaultValue ....... : $defaultValue');
    // If something goes wrong we return the default value.
    return defaultValue;
  }
  // If we did not have a type converter, we return the default value.
  return defaultValue;
}