value property
Gets the integer value of this field.
If the underlying rawValue is null
, this getter returns 0
by default.
This ensures that the field always provides an integer value when accessed.
Implementation
@override
int get value => rawValue ?? 0;
Sets the value of this field.
The value
can be an int
, String
, or null
.
- If
value
is anint
ornull
, it is directly assigned to rawValue. - If
value
is aString
, it attempts to parse it into anint
usingint.tryParse
.
Examples:
JsonInteger ageField = JsonInteger("age");
// Setting with an integer
ageField.value = 25; // rawValue is 25
// Setting with a numeric string
ageField.value = "30"; // rawValue is 30
// Setting with a non-numeric string
ageField.value = "abc"; // rawValue is null
Implementation
@override
set value(dynamic value) {
if (value is int?) {
rawValue = value;
return;
}
if (value is String) {
rawValue = int.tryParse(value);
return;
}
}