Why This Happens
In Dart, types are strict and implicit conversions do not happen. This commonly occurs when JSON data contains a number as a string (e.g., "42" instead of 42) but the Dart code expects an int. Use int.parse() or int.tryParse() for conversion.
The Problem
final json = {'count': '42'};
final int count = json['count']; // String is not intThe Fix
final json = {'count': '42'};
final int count = int.parse(json['count']);Step-by-Step Fix
- 1
Identify the error
Look at the error message: type 'String' is not a subtype of type 'int'. This means a String was assigned where an int was expected.
- 2
Find the cause
Check JSON responses or dynamic data where numbers might be represented as strings.
- 3
Apply the fix
Use int.parse() or int.tryParse() for safe conversion, and handle potential FormatException.
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