letAsStringOrNone function

Option<String> letAsStringOrNone(
  1. dynamic input
)

Converts input to String, returning None on failure.

Supported types:

Implementation

Option<String> letAsStringOrNone(dynamic input) {
  if (input is Outcome) {
    return switch (input.rawSync().value) {
      Ok(value: final okValue) => letAsStringOrNone(
          NoStackOverflowWrapper(okValue),
        ),
      Err() => const None(),
    };
  }
  final rawInput = input is NoStackOverflowWrapper ? input.value : input;
  // Treat `null` as absence (None) rather than producing the literal string
  // "null" — `null.toString() == 'null'` is rarely what callers want.
  if (rawInput == null) return const None();

  try {
    return Some(rawInput.toString());
  } catch (_) {
    return const None();
  }
}