value property
Returns the numeric value of this field.
If the underlying rawValue is null, this getter returns 0 as
a default value. This ensures that number fields always provide a
valid numeric value when accessed, which is useful for calculations.
Returns:
The numeric value (can be int or double), or 0 if rawValue is null.
Example:
final field = JsonNumber('count');
print(field.value); // 0 (default)
field.value = 42;
print(field.value); // 42
Implementation
@override
num get value => rawValue ?? 0;
Sets the numeric value, accepting both numbers and numeric strings.
This setter provides flexible input handling:
numornull: Assigned directly to rawValue (accepts bothintanddoublesince they extendnum)String: Parsed usingnum.tryParse(). If parsing fails, rawValue is set tonull
Parameters:
value: The value to set, which can be anum(int or double), a numericString, ornull.
Example:
final field = JsonNumber('count');
field.value = 42; // Integer
field.value = 3.14; // Double
field.value = "100"; // String → 100
field.value = "invalid"; // Sets rawValue to null
Implementation
@override
set value(dynamic value) {
if (value is num?) {
rawValue = value;
return;
}
if (value is String) {
rawValue = num.tryParse(value);
return;
}
}