convertStringToType<T> function

T? convertStringToType<T>(
  1. String value,
  2. Type targetType
)

Converts a string to the appropriate type

Implementation

T? convertStringToType<T>(String value, Type targetType) {
  if (targetType == String) return value as T?;
  if (targetType == int) return int.tryParse(value) as T?;
  if (targetType == double) return double.tryParse(value) as T?;
  if (targetType == bool) {
    final lower = value.toLowerCase();
    if (lower == 'true') return true as T?;
    if (lower == 'false') return false as T?;
    return null;
  }
  return null;
}