parseDuration static method

Duration? parseDuration(
  1. String? raw
)

Parses a simple duration token such as 500ms, 10s, or 2m.

Implementation

static Duration? parseDuration(String? raw) {
  if (raw == null || raw.trim().isEmpty) return null;
  final value = raw.trim();
  final match = RegExp(r'^(\d+)(ms|s|m)?$').firstMatch(value);
  if (match == null) return null;
  final amount = int.parse(match.group(1)!);
  final unit = match.group(2) ?? 's';
  switch (unit) {
    case 'ms':
      return Duration(milliseconds: amount);
    case 's':
      return Duration(seconds: amount);
    case 'm':
      return Duration(minutes: amount);
    default:
      return null;
  }
}