rAutoCast function

dynamic rAutoCast(
  1. dynamic value
)

Implementation

dynamic rAutoCast(dynamic value) {
  if (value is String) {
    // Attempt to cast to int
    final intValue = int.tryParse(value);
    if (intValue != null) return intValue;

    // Attempt to cast to double
    final doubleValue = double.tryParse(value);
    if (doubleValue != null) return doubleValue;

    // Attempt to cast to bool
    if (value.toLowerCase() == 'true') return true;
    if (value.toLowerCase() == 'false') return false;
  }
  // If no casting is applicable, return the original element
  return value;
}