jsonDecodeNullableInt function

int? jsonDecodeNullableInt(
  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.

jsonDecodeNullableInt('42'); // 42

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

Implementation

int? jsonDecodeNullableInt(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,
};