value property

  1. @override
int get value
override

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;
  1. @override
set value (dynamic value)
override

Sets the value of this field.

The value can be an int, String, or null.

  • If value is an int or null, it is directly assigned to rawValue.
  • If value is a String, it attempts to parse it into an int using int.tryParse.
    • If parsing succeeds, the parsed integer is assigned to rawValue.
    • If parsing fails (e.g., the string is non-numeric), rawValue is set to null.

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;
  }
}