type 'int' is not a subtype of type 'String'

type 'int' is not a subtype of type 'String'

Quick Answer

A value of type int was used where a String was expected.

Why This Happens

In Dart, implicit type conversions do not happen. When JSON data contains an integer but your code expects a String, this runtime type error occurs. Use .toString() for explicit conversion or check the type before casting.

The Problem

final json = {'id': 123};
final String id = json['id']; // int is not String

The Fix

final json = {'id': 123};
final String id = json['id'].toString();

Step-by-Step Fix

  1. 1

    Identify the error

    Look at the error message: type 'int' is not a subtype of type 'String'. This means an int value was assigned to a String variable.

  2. 2

    Find the cause

    Check JSON parsing or dynamic data handling where the actual type differs from the expected type.

  3. 3

    Apply the fix

    Use .toString() for explicit conversion, or parse the JSON with proper type handling in your fromJson factory.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free