Skip to content

Lists created with list.cast() don't work translate to Arrays on js-side with js-interop #49271

Open
@a14n

Description

@a14n

With the following html file:

<script>
  function f(list) {
    console.log(list);
    console.log(typeof(list[0]));
  }
</script>
<script defer src="page.dart.js"></script>

and the following dart file:

import 'dart:js_util';
import 'package:js/js.dart';

@JS()
external Object get window;

void main() {
  var l = <dynamic>[1, 2, 3];
  callMethod(window, 'f', [l]);                      // OK
  callMethod(window, 'f', [l.cast<int>()]);          // ERROR
  callMethod(window, 'f', [l.cast<int>().toList()]); // OK
}

I get the following console output on chrome:

Array(3)
number
CastList.new
undefined
Array(3)
number

As you can see using only l.cast<int>() leads to unusable array on js side. A workaround is to call .toList() to get back an Array.

Version :

$ dart --version
Dart SDK version: 2.17.3 (stable) (Wed Jun 1 11:06:41 2022 +0200) on "linux_x64"

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-web-jsIssues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop.web-js-interopIssues that impact all js interopweb-triage-0repro is available

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions