# Dart: type 'Null' is not a subtype of type 'String' in type cast

**Code:** `type 'Null' is not a subtype of type 'String'` · **Category:** Flutter / Dart · **Seen on:** Flutter

A value that was `null` at runtime was cast or used as a non-nullable type, most often when parsing JSON.

## Meaning

Dart's sound null safety makes `as String` fail if the key is missing or null. Make the field nullable (`String?`), provide a default (`json['name'] ?? ''`), or validate the payload. The message names the target type; the stack trace points to the line.

## Common causes

- JSON key missing or null cast with `as String`
- fromJson with required non-nullable fields
- API returning null for optional data
- Wrong key name (typo)
- Platform channel returning null

## Quick fix

1. Use nullable types or `??` defaults
2. Validate the JSON before casting
3. Use code generation (json_serializable) with `@JsonKey(defaultValue: …)`

## Detailed fix by platform

### Dart

1. `name: json['name'] as String? ?? ''`

## How to diagnose

1. **Key** — Present in the JSON?
2. **Type** — Nullable in model?
3. **Default** — Provided?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/flutter/type-null-is-not-a-subtype)
