create<T extends Enum> static method

Future<TSharedEnumPod<T>> create<T extends Enum>(
  1. String key, {
  2. required Iterable<T> options,
  3. required T initialValue,
})

Creates a pod that persists an enum value.

Requires a non-nullable initialValue to ensure type safety, as there is no universal default for enums.

Implementation

static Future<TSharedEnumPod<T>> create<T extends Enum>(
  String key, {
  required Iterable<T> options,
  required T initialValue,
}) {
  return TSharedEnumPod.create(
    key,
    fromValue: (rawValue) {
      if (rawValue == null) return initialValue;
      // Find the enum by its name, or fall back to the initialValue.
      return options.firstWhere(
        (e) => e.name.toLowerCase() == rawValue.toLowerCase(),
        orElse: () => initialValue,
      );
    },
    toValue: (value) => value.name,
    initialValue: initialValue,
  );
}