Number static method
Returns a validator that checks if a string is a valid number.
Arguments :
errorMessage
: The error message to return if the string is not a valid number.next
: A validator to run after this validator.integerOnly
: If set to true, the validator will only accept integers.allowNegative
: If set to false, the validator will not accept negative numbers.allowDecimal
: If set to false, the validator will not accept decimal numbers.
Usage :
TextFormField(
validator: Validators.Number(),
),
Implementation
static String? Function(String? value)? Number({
String errorMessage = 'Invalid number',
String? Function(String value)? next,
bool integerOnly = false,
bool allowNegative = true,
bool allowDecimal = true,
}) {
assert(
!integerOnly || (integerOnly && !allowDecimal),
'integerOnly and allowDecimal cannot both be true. '
'If you want to allow decimal numbers, set integerOnly to false');
return (value) {
if (value == null || value.trim().isEmpty) {
return null;
}
if (integerOnly) {
if (int.tryParse(value) == null) {
return errorMessage;
}
} else {
if (num.tryParse(value) == null) {
return errorMessage;
}
}
if (!allowNegative && value.startsWith('-')) {
return errorMessage;
}
if (!allowDecimal && value.contains('.')) {
return errorMessage;
}
if (next != null) {
return next(value);
}
return null;
};
}