intNullableFromJson function

int? intNullableFromJson(
  1. dynamic value
)

Converts a dynamic value to a nullable int.

Returns null if the input is null or cannot be converted to an int.

final result = intNullableFromJson('42');
print(result); // 42

@ai Use this method when you want to allow null values in your numeric data.

Implementation

int? intNullableFromJson(final dynamic value) => switch (value) {
      final int value => value,
      final double value => value.toInt(),
      final String value => () {
          final doubleSeconds = double.tryParse(value);
          if (doubleSeconds != null) return doubleSeconds.toInt();

          final intSeconds = int.tryParse(value);
          if (intSeconds != null) return intSeconds;

          return null;
        }(),
      _ => null,
    };