toInt method

  1. @Deprecated('This method is deprecated and will be removed in a future release. ' 'Please use other type conversion methods or handle conversions manually.')
int toInt()

Converts the value to an int.

  • Returns 0 if the value is null.
  • Returns the value if it's already an int.
  • Converts and returns the value if it's a double or a String.
  • Throws a FormatException if conversion is not possible.

Implementation

@Deprecated(
  'This method is deprecated and will be removed in a future release. '
  'Please use other type conversion methods or handle conversions manually.',
)
int toInt() {
  if (this == null) {
    return 0;
  } else if (this is int) {
    return this as int;
  } else if (this is double) {
    return (this as double).toInt();
  } else if (this is String) {
    return int.tryParse(this as String) ?? 0;
  } else {
    throw FormatException('Cannot convert $this to int');
  }
}