dart-lang / web

Lightweight browser API bindings built around JS static interop.
https://pub.dev/packages/web
BSD 3-Clause "New" or "Revised" License
135 stars 23 forks source link

question: build error while adding an event listener #227

Closed caleb-at-pieces closed 6 months ago

caleb-at-pieces commented 6 months ago

I am attempting to add an event listener on the window like so:

import 'dart:html';
import 'dart:js_interop';
import 'package:web/web.dart' as js;

void main() {
  runApp(const MyApp());
  try {
    js.window.addEventListener('message', test.toJS);
  } catch (e) {
    print('there was an err $e');
  }
}

void test(Event _) {
  print('test invoked');
}

however whenever I try to build my application I get this build error:

image

I saw someone faced this same issue here https://github.com/dart-lang/web/issues/212#issuecomment-2041106649 however after trying to replicate their code I still face the same issue

caleb-at-pieces commented 6 months ago

alright my issue was I was importing the 'Event' type from the dart:html package instead of the package:web/web.dart package

I'm all good now!

srujzs commented 6 months ago

If you upgrade your SDK to something like 3.4, we've actually enabled this behavior (to use a dart:html type in a function that gets toJS'd) so that code can be a bit easier to migrate from dart:html to package:web. Granted, dart:html still won't work on dart2wasm. At any rate, it looks like using the dart:html type wasn't your intention anyways.