# type 'Null' is not a subtype of type 'int' in type cast

- **ID:** `flutter/typeerror-null-not-subtype-int`
- **Domain:** flutter
- **Category:** type_error
- **Verification:** ai_generated
- **Fix Rate:** 88%

## Root Cause

A null value was assigned or returned where a non-nullable int was expected, often due to missing null-safety handling in JSON parsing or API responses.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Flutter 3.0 | active | — | — |
| Flutter 3.3 | active | — | — |
| Flutter 3.7 | active | — | — |

## Workarounds

1. **Use null-safe access with a fallback value:
int value = (json['field'] as int?) ?? 0;** (90% success)
   ```
   Use null-safe access with a fallback value:
int value = (json['field'] as int?) ?? 0;
   ```
2. **Use a JSON parsing library like 'json_serializable' with nullable fields defined properly:
@JsonKey(name: 'field', defaultValue: 0)
int field;** (95% success)
   ```
   Use a JSON parsing library like 'json_serializable' with nullable fields defined properly:
@JsonKey(name: 'field', defaultValue: 0)
int field;
   ```
3. **Add explicit null checks before casting:
if (json['field'] != null) {
  int value = json['field'] as int;
} else {
  // handle null case
}** (85% success)
   ```
   Add explicit null checks before casting:
if (json['field'] != null) {
  int value = json['field'] as int;
} else {
  // handle null case
}
   ```

## Dead Ends

- **Using 'as int' without null check** — If the value is null, the cast will throw the same error. (90% fail)
- **Ignoring null-safety and using 'dynamic' instead of 'int'** — Defeats the purpose of null safety and can lead to runtime type errors elsewhere. (70% fail)
- **Assuming the API will never return null for that field** — APIs can change or return null for missing fields; brittle assumption. (80% fail)
