Skip to content

Conversation

@lemz90
Copy link
Contributor

@lemz90 lemz90 commented Nov 21, 2025

Problem

Building for web with the --wasm flag fails in Flutter 3.38+ with the following error:

type 'NullConstant' is not a subtype of type 'IntConstant' in type cast
Exception in SwitchStatement at build_op.dart:172:17

The root cause is the switch statement at build_op.dart:172-183 that mixes null and integer case values. The dart2wasm compiler in Dart 3.10 has stricter internal type checking for switch statement constants (though this is not documented as a breaking change).

Solution

Refactored the problematic switch statement to use if-else logic that explicitly handles the nullable type:

Before:

switch (children?.length) {
  case null:
    return placeholder;
  case 0:
    return widget0;
  case 1:
    return children?.first ?? widget0;
  default:
    throw UnsupportedError(...);
}

After:

final length = children?.length;
if (length == null) {
  return placeholder;
} else if (length == 0) {
  return widget0;
} else if (length == 1) {
  return children!.first;
} else {
  throw UnsupportedError(
    'onWidgets must return exactly 1 widget, got $length',
  );
}

This approach:

  • Explicitly separates null handling from value checking
  • Allows the compiler to properly type-promote length to non-null int
  • Maintains identical functionality while satisfying stricter compiler requirements

Testing

  • ✅ Tested with Flutter 3.38.2 / Dart 3.10.0
  • ✅ Successfully builds with flutter build web --release --wasm
  • ✅ Existing functionality preserved
  • ✅ No behavioral changes

Related Issue

Fixes #1528

@lemz90
Copy link
Contributor Author

lemz90 commented Nov 21, 2025

I've fixed the failing tests by updating expectations for NetworkImage.toString() in Flutter 3.38+.

Background on webHtmlElementStrategy

This parameter was added to NetworkImage in Flutter 3.29, and Flutter 3.36 updated toString() to include it for better debugging. The default value (WebHtmlElementStrategy.never) has always been used implicitly by this project, so there's no behavioral change - just visibility in debug output.

However, since this parameter controls how images render on Flutter Web (canvas-based vs HTML <img> elements), it may be worth reviewing whether other strategies could benefit the project.

Test Fixes

Updated 8 test files in packages/core/test/ to match the new toString format:

  • Expected: NetworkImage("url", scale: 1.0, webHtmlElementStrategy: never, headers: null)
  • Previously: NetworkImage("url", scale: 1.0)

Audio Test Note

The fwfh_just_audio test failure (ink_sparkle.frag shader error) is unrelated to these changes - I verified it also fails on the base branch. This appears to be a Material 3 widget test issue in Flutter 3.38.


Summary: This PR now fixes both the dart2wasm switch statement in dart2wasm compilation error and updates tests for Flutter 3.38 compatibility.

@github-actions
Copy link

@daohoangson daohoangson merged commit 15106a9 into daohoangson:master Nov 24, 2025
13 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dart2wasm compilation fails in Flutter 3.38+ due to mixed null/int switch cases

2 participants