Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Flutter and app store releases

Dart throws type 'Null' is not a subtype of type 'bool' at runtime

Your error

type 'Null' is not a subtype of type 'bool'3 more lines it can show

Fix it fast

Most common cause, 1 of 3

A JSON or Firestore field is missing or null and is read as bool

Read the field as a nullable bool and decide what a missing value means, usually false.

Fix
factory User.fromJson(Map<String, dynamic> json) => User(
      name: json['name'] as String? ?? '',
      isActive: json['isActive'] as bool? ?? false,
    );
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

type 'Null' is not a subtype of type 'bool' is a failed runtime cast: a dynamic value, such as a decoded JSON field or an untyped showDialog result, was null and Dart cast it to bool. Read it as bool? with a default: json['active'] as bool? ?? false. Inside Text() the same mistake names 'String'. Dart 3.x, Flutter 3.x.

  • Applies to Dart 3.0 to 3.14, Flutter 3.38 to 3.50.1
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Read the error together with the first stack frame below it. A trailing in type cast means an explicit as bool; a frame in fromJson or a map read points to the first cause, a line that awaits showDialog or Navigator.push to the second, and a build method with Text to the third.

Run this first
flutter run 2>&1 | grep -A8 "is not a subtype of type"

Then find the line you see in this list and open its fix:

  1. 1A JSON or Firestore field is missing or null and is read as boolThe top frame is a fromJson factory or a line like data['isActive'] as bool
  2. 2An untyped dialog or route result is used as a conditionThe top frame is the if line right after await showDialog(...) or await Navigator.push(...), and it fails when the user taps outside the dialog or presses back
  3. 3A null value reaches a Text widgetThe message says type 'String' instead of 'bool', and the frame is inside a build method

What does the error look like?

  • type 'Null' is not a subtype of type 'bool'
  • type 'Null' is not a subtype of type 'bool' in type cast
  • type 'Null' is not a subtype of type 'String'
  • A non-null String must be provided to a Text widget.

Cause 1 of 3

A JSON or Firestore field is missing or null and is read as bool

You seeThe top frame is a fromJson factory or a line like data['isActive'] as bool

Check

type 'Null' is not a subtype of type 'bool' from a model class means a map lookup returned null. jsonDecode and DocumentSnapshot.data() give dynamic values, and a Map returns null for a key it does not contain. Assigning that dynamic value to a bool field inserts a runtime cast, which throws as soon as the value is null, whether you wrote as bool or not.

Check
final map = jsonDecode(body) as Map<String, dynamic>;
debugPrint('isActive: ${map['isActive']} (${map['isActive'].runtimeType})');

Fix

Read the field as a nullable bool and decide what a missing value means, usually false. Keep the non-nullable field in the model so the rest of the app stays simple.

Fix
factory User.fromJson(Map<String, dynamic> json) => User(
      name: json['name'] as String? ?? '',
      isActive: json['isActive'] as bool? ?? false,
    );

Verify

A unit test that decodes a response without the isActive key builds the model with isActive set to false.

Verify
flutter test test/user_test.dart

Confirmed by dart.dev

Did this fix it?

Cause 2 of 3

An untyped dialog or route result is used as a condition

You seeThe top frame is the if line right after await showDialog(...) or await Navigator.push(...), and it fails when the user taps outside the dialog or presses back

Check

showDialog and Navigator.push complete with whatever value was passed to Navigator.pop, and with null when the route closes without one, which is what a barrier tap or the back button does. Called without a type argument, the result is dynamic, so if (confirmed) casts that null to bool and throws.

Check
grep -rn -E "await (showDialog|Navigator\.(of\(context\)\.)?push)\(" lib/

Fix

Pass the result type, then treat null as a no. The analyzer now sees bool? and asks you to handle the null case.

Fix
final confirmed = await showDialog<bool>(
  context: context,
  builder: (context) => AlertDialog(
    title: const Text('Delete this item?'),
    actions: [
      TextButton(onPressed: () => Navigator.pop(context, false), child: const Text('Cancel')),
      TextButton(onPressed: () => Navigator.pop(context, true), child: const Text('Delete')),
    ],
  ),
);
if (confirmed ?? false) {
  await deleteItem();
}

Verify

Open the dialog and dismiss it by tapping the barrier: nothing is deleted and no exception appears.

Verify
flutter run

Confirmed by api.flutter.dev

Did this fix it?

Cause 3 of 3

A null value reaches a Text widget

You seeThe message says type 'String' instead of 'bool', and the frame is inside a build method

Check

Flutter 1.x stopped a null Text with the assertion A non-null String must be provided to a Text widget. Today the Text constructor takes a non-nullable String, so a typed String? is caught by the analyzer, while a dynamic value, such as user['name'] from a decoded map, fails at runtime with type 'Null' is not a subtype of type 'String'.

Check
grep -rn -E "Text\([a-zA-Z_]+\[" lib/

Fix

Turn the dynamic value into a String before the widget sees it, with a fallback for null. Better still, convert the map into a typed model once and pass its String fields to widgets.

Fix
Text(user['name'] as String? ?? 'Unknown')
// or, with a typed model:
Text(profile.displayName)

Verify

The screen renders with the fallback text when the field is missing.

Verify
flutter run

Confirmed by api.flutter.dev

Did this fix it?

How do I stop it coming back?

  • Turn on strict-casts in analysis_options.yaml (analyzer: language: strict-casts: true) so every place where a dynamic value flows into bool or String is reported before runtime.
  • Always give showDialog, showModalBottomSheet and Navigator.push a type argument such as <bool>, and compare the result with == true or ?? false.
  • Keep a fromJson test per model that decodes a sample with optional fields left out.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, Qist Bazaar, SessionBird.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.