defaultconverters property

Map<String, Object? Function(dynamic value)> defaultconverters
final

Implementation

static final Map<String, Object? Function(dynamic value)> defaultconverters = {
  'string': _defaultStringConverter,
  'string_uppercase': (dynamic value) =>
      _defaultStringConverter(value)?.toUpperCase(),
  'string_lowercase': (dynamic value) =>
      _defaultStringConverter(value)?.toLowerCase(),
  'number': (dynamic value) {
    if (value is num) {
      return value;
    }
    if (value is String) {
      return num.tryParse(value);
    }
    if (value is DateTime) {
      return value.millisecondsSinceEpoch;
    }
    return null;
  },
  'double': (dynamic value) {
    if (value is double) {
      return value;
    }
    if (value is int) {
      return value.toDouble();
    }
    if (value is String) {
      return double.tryParse(value);
    }
    if (value is DateTime) {
      return value.microsecondsSinceEpoch.toDouble();
    }
    return null;
  },
  'bool': (dynamic value) {
    if (value is bool) {
      return value;
    }
    if (value is double) {
      return value < 1;
    }
    if (value is int) {
      return value > 0;
    }
    if (value is String) {
      final lowerCase = value.toLowerCase();
      return lowerCase.startsWith('t') ||
          value.startsWith('y') ||
          value.startsWith('on');
    }
    return null;
  },
  'datetime': (dynamic value) {
    if (value is DateTime) {
      return value;
    }
    if (value is String) {
      return DateTime.tryParse(value);
    }
    if (value is int) {
      return DateTime.fromMillisecondsSinceEpoch(value);
    }
    if (value is double) {
      return DateTime.fromMicrosecondsSinceEpoch(value.toInt());
    }
    return null;
  },
  'duration': (dynamic value) {
    if (value is Duration) {
      return value;
    }
    if (value is String) {
      final milliseconds = int.tryParse(value);
      if (milliseconds != null) {
        return Duration(milliseconds: milliseconds);
      }
    }
    if (value is num) {
      return Duration(milliseconds: value.toInt());
    }
    if (value is DateTime) {
      throw Exception('Cannot convert DateTime to Duration');
    }
    return null;
  },
};