safeCast<T> static method
T?
safeCast<T>(
- dynamic value
Safely casts a value to a type, returns null if cast fails
Example:
Helpers.safeCast<num>('123'); // null
Helpers.safeCast<num>(123); // 123
Implementation
static T? safeCast<T>(dynamic value) {
try {
return value as T;
} catch (_) {
return null;
}
}