Skip to content

Issue: Dart Compile JS Fails to Assign External JS Function #56533

@mrtnetwork

Description

@mrtnetwork

Description

I'm encountering an issue when compiling my Dart code to JavaScript using dart compile js. The problem appears to be related to the assignment of an external JavaScript function via dart:js_interop.

Code Example
Here's a minimal example of the code causing the issue:

import 'dart:js_interop';

@JS("exportedFunction")
external set exportedFunctions(JSFunction value);

void printStrings(JSString string) {}

void main() {
  print("start!");
  try {
    exportedFunctions = printStrings.toJS;
    print("function initialized.");
  } catch (e) {
    print("something went wrong: $e");
  }
  print("done");
}

Expected Output

start!
function initialized.
done

Actual Output
Without the try-catch block:

start!

With the try-catch block:

start!
done

Additional Information
Dart SDK Version: 3.5.0 (stable) (Tue Jul 30 02:17:59 2024 -0700) on "macos_x64"

Compilation Command:

dart compile js -o ./build.js ./build.dart

When using the -O0 flag or compiling to WebAssembly (dart compile wasm), the code works perfectly.

Steps Taken to Resolve
Removed and reinstalled both the Flutter and Dart SDKs.
Cleared cache.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-web-jsIssues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop.type-bugIncorrect behavior (everything from a crash to more subtle misbehavior)web-dart2jsweb-js-interopIssues that impact all js interop

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions