Incorrect comparison of two key codes in an asynchronous function after dart2js

I do not understand this dart2js code behavior.
I have this only in async function and only after compilation in JS.

e.keyCode is equal 13 KeyCode.ENTER is equal 13 

but

 (e.keyCode == KeyCode.ENTER) is false 

This is simple code to debug my problem.
What's happening?

 import 'dart:html'; main() async { await for(KeyboardEvent e in window.onKeyDown) { print('e.keyCode : ${e.keyCode}'); print('e.keyCode.hashCode : ${e.keyCode.hashCode}'); print('KeyCode.ENTER : ${KeyCode.ENTER}'); print('KeyCode.ENTER.hashCode : ${KeyCode.ENTER.hashCode}'); print('e.keyCode.runtimeType : ${e.keyCode.runtimeType}'); print('KeyCode.ENTER.runtimeType : ${KeyCode.ENTER.runtimeType}'); print('e.keyCode == KeyCode.ENTER ${e.keyCode == KeyCode.ENTER}'); print('e.keyCode != KeyCode.ENTER ${e.keyCode != KeyCode.ENTER}'); int a = e.keyCode; int b = KeyCode.ENTER; print('a = $a'); print('b = $b'); print('a.hashCode = ${a.hashCode}'); print('b.hashCode = ${b.hashCode}'); print('a == b ${(a == b).toString()}'); print('a == 13 ${(a == 13).toString()}'); print('b == 13 ${(b == 13).toString()}'); if(a == b) print('DART: a == b'); else print('DART: a != b'); } } 

Exit to Chrome after pressing Enter (dart2js - minified):

e.keyCode: 13
e.keyCode.hashCode: 13
KeyCode.ENTER: 13
KeyCode.ENTER.hashCode: 13
e.keyCode.runtimeType: int
KeyCode.ENTER.runtimeType: int
e.keyCode == KeyCode.ENTER false
e.keyCode! = KeyCode.ENTER true
a = 13
b = 13
a.hashCode = 13
b.hashCode = 13
a == b true
a == 13 true
b == 13 true
DART: a! = B

In DartVM (Dartium), everything is correct:

e.keyCode: 13
e.keyCode.hashCode: 13
KeyCode.ENTER: 13
KeyCode.ENTER.hashCode: 13
e.keyCode.runtimeType: int
KeyCode.ENTER.runtimeType: int
e.keyCode == KeyCode.ENTER true
e.keyCode! = KeyCode.ENTER false
a = 13
b = 13
a.hashCode = 13
b.hashCode = 13
a == b true
a == 13 true
b == 13 true
DART: a == b

// EDIT
I noticed that it doesn't matter that I use keyCode.
This is an asynchronous issue.
The code below returns “OK” on Dartium and “NOPE” in Chrome after compilation in JS.

 import 'dart:async'; main() async { var ctrl = new StreamController(); ctrl.add(true); await for(var e in ctrl.stream) { if(e == e) print('OK'); else print('NOPE'); } } 
+6
source share
2 answers

This is really the same as this mistake .

The wrong type was inferred for the async iteration variable for loops.

It is fixed in 1.10.

+3
source

This should be error 1.9.3 dart2js.

Now I am using the Dart SDK version 1.10.0-dev.1.5 and everything works fine. This is only the solution that I found if I want to use "wait."

+1
source

Source: https://habr.com/ru/post/985397/


All Articles