value property
Returns the double value of this field.
If the underlying rawValue is null, this getter returns 0.0 as
a default value. This ensures that double fields always provide a
valid numeric value when accessed, which is useful for calculations.
Returns:
The double value, or 0.0 if rawValue is null.
Example:
final field = JsonDouble('price');
print(field.value); // 0.0 (default)
field.value = 19.99;
print(field.value); // 19.99
Implementation
@override
double get value => rawValue ?? 0;
Sets the double value, accepting multiple input types with automatic conversion.
This setter provides flexible input handling:
double: Assigned directly to rawValueint: Converted todoubleusingtoDouble()String: Parsed usingdouble.tryParse(). If parsing fails, rawValue is set tonullnull: Sets rawValue tonull- Other types: Attempts conversion via
toString()and parsing. If that fails, rawValue is set tonull
Parameters:
value: The value to set, which can be adouble,int,String,null, or other types (with fallback conversion).
Example:
final field = JsonDouble('price');
field.value = 19.99; // Direct double
field.value = 25; // Integer → 25.0
field.value = "29.99"; // String → 29.99
field.value = "invalid"; // Sets rawValue to null
Implementation
@override
set value(dynamic value) {
if (value == null) {
rawValue = null;
return;
}
if (value is double) {
rawValue = value;
return;
}
if (value is int) {
rawValue = value.toDouble();
return;
}
if (value is String) {
rawValue = double.tryParse(value);
return;
}
// Attempt to handle other data types via conversion
try {
rawValue = double.tryParse(value.toString());
} catch (_) {
rawValue = null; // Fallback if conversion fails
}
}