coerceValue function

Object? coerceValue(
  1. String text,
  2. ColumnType type
)

Parses editor text back into the column's value type.

Editors hand back strings; a number column must not silently store "42" where every comparator expects a num.

A number must be a finite decimal. num.tryParse also accepts 0x10, NaN and Infinity, so pasting any of those into a numeric column stored a value no user typed - 16 for 0x10 - or one that makes every sum, average and sort over the column meaningless.

Implementation

Object? coerceValue(String text, ColumnType type) {
  final trimmed = text.trim();
  if (trimmed.isEmpty) return null;
  switch (type) {
    case ColumnType.number:
      return parseDecimal(trimmed);
    case ColumnType.boolean:
      final lower = trimmed.toLowerCase();
      if (lower == 'true' || lower == 'yes' || lower == '1') return true;
      if (lower == 'false' || lower == 'no' || lower == '0') return false;
      return null;
    case ColumnType.date:
      return DateTime.tryParse(trimmed);
    case ColumnType.text:
    case ColumnType.custom:
      return text;
  }
}