Skip to content

Type cast errors in Flutter Web release builds (dart2js/dart2wasm) #355

@JacobGood1

Description

@JacobGood1

Summary
ClojureDart-generated Dart code fails with type cast errors when compiled for Flutter Web in release mode, but works correctly in debug and profile modes.

Environment

  • Flutter SDK: 3.10.4+
  • Platform: Flutter Web
  • ClojureDart: latest
  • OS: Windows

Steps to Reproduce

  • Create a ClojureDart Flutter project with standard data structures (records, atoms, persistent hash maps)
  • Run flutter run -d chrome → Works correctly
  • Run flutter build web then deploy → Fails with type errors
  • Run flutter build web --profile then deploy → Works correctly

Error Messages

  • dart2js (release) build:

TypeError: Instance of 'minified:ba': type 'minified:ba' is not a subtype of type 'num'

  • dart2wasm (--wasm) build:

Type 'minified:Class1412' is not a subtype of type 'minified:Class2786' in type cast

Suspected Cause
The error occurs in ClojureDart's core data structures when values are cast to num (e.g., (value as dc.num).toDouble()). In debug mode, the types are correct, but dart2js/dart2wasm's aggressive optimizations in release mode appear to break type inference, causing incorrect types to flow through the cast expressions.

Example from generated code:

.apply(
  fontSizeFactor: ((const lcoc_core.Keyword(null, "font-size-factor", ...).$_invoke$1(lcoc_main.theme$v1, )) as dc.num).toDouble(),
  fontSizeDelta: ((const lcoc_core.Keyword(null, "font-size-delta", ...).$_invoke$1(lcoc_main.theme$v1, )) as dc.num).toDouble(),
)

The $_invoke$1 call returns a value from a PersistentHashMap, and the cast to dc.num fails in optimized builds.

Workaround
Building with flutter build web --profile produces a working build, though it's larger and not ideal for production.

Additional Context
The app loads partially (some widgets renders) before crashing
The issue affects PersistentHashMap lookups and possibly other core data structure operations
Tested with both dart2js (default) and dart2wasm (--wasm) - both fail similarly

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions