-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Cast error when awaiting enumerateDevices() #39627
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@terrylucas - this appears like an error in the generated dart:html code. I believe we'll hit this issue in other APIs as well, basically anywhere we have |
Bug: #39627 Enumerating media devices gives back a cast error in html_dart2js. This test should fail and the cast should be fixed such that it passes. Change-Id: Ie2502f265448d078578113cbea57dd81d20015c7 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/128104 Commit-Queue: Srujan Gaddam <[email protected]> Reviewed-by: Sigmund Cherem <[email protected]>
Bug: #39627 Lists in a future should be typed with dynamic since they come from JS interop. Results from running tools/dom/scripts/go.sh Change-Id: I4ed37d6f5fa570beef71652dcad17c3bcf7560ab Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/128300 Reviewed-by: Sigmund Cherem <[email protected]> Commit-Queue: Srujan Gaddam <[email protected]>
Bug: #39627 Lists in a future should be typed with dynamic since they come from JS interop. Change-Id: I93d4da16eb27c3af23820a170cfad7cc5c3b4472 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/128368 Reviewed-by: Sigmund Cherem <[email protected]> Commit-Queue: Srujan Gaddam <[email protected]>
Can we make JavaScript |
is |
Bug: #39627 Changelog reflects changes in https://dart-review.googlesource.com/c/sdk/+/128368. Change-Id: Idace6e7c7af81ebd9e0b61639e239205e55e53d8 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/128640 Commit-Queue: Srujan Gaddam <[email protected]> Reviewed-by: Sigmund Cherem <[email protected]>
I'm going to mark this as closed since the original issue has been addressed. Please feel free to reopen or open a separate issue if this can be cleaned up. |
I see this is closed and it was noted that the original issue has been addressed. Does that mean that the code above now returns the expected types? e.g. Can I do: import 'dart:html';
main()
{
final devices = await window.navigator.mediaDevices.enumerateDevices();
print(devices.first.runtimeType); // MediaDeviceInfo
print(devices.first.deviceId);
} |
@mnordine - I haven't verified this, but yes, I expect that the individual elements will contain the expected type. final devices = await window.navigator.mediaDevices.enumerateDevices();
print(decices.runtimeType); // List<dynamic>
print(devices.first.runtimeType); // MediaDeviceInfo because the list type is dynamic and not devices.first.nonExistingProperty You can however use the static type to avoid this problem: MediaDeviceInfo device = devices.first;
print(device.deviceId); Note that the fix landed in b84d6ba, which will be included in the next dev channel release of the sdk. |
Sorry if I'm misunderstanding, but was the solution here to make all web apis that return lists return them as If so, is this temporary? Also, I don't understand how, if |
Correct, this was necessary because of how types are represented in the JavaScript program. When you create a list by hand in Dart, our compiler generates code that will store the type-argument together with that list. However, when the list is created outside the Dart program (e.g. from DOM APIs in the browser, or other JavaScript libraries), the list is a plain JavaScript arrays which is not tagged in any way. An alternative approach is to wrap the lists to provide a different static type, but we have learned in the past that wrapping on every API call is expensive. Modifying the underlying JavaScript array to include our type-parameter tag is sometimes not safe (since it can affect the external JavaScript code in the case of JS-interop), but we should look more into whether this is possible in some cases like tehse. For now, we adjusted the type to represent more closely what the browser is giving us: an untagged list, which today we interpret as a
This is unfortunate, yes. In the future, we are planning to have a different static type for this
When we say It so happens that we know from the browser that all instances in that list will be instances of final devices = (await window.navigator.mediaDevices.enumerateDevices()).cast<MediaDeviceInfo>();
print(decices.runtimeType); // List<MediaDeviceInfo>
print(devices.first.runtimeType); // MediaDeviceInfo (* this is the kind of wrapping operation that we could have added automatically, but we didn't to avoid the performance cost... it's not out of the question to reevaluate the tradeoffs and reconsider this) |
Appreciate the explanation and the
Given the above, why can't it be I think I understand you're saying you don't want users to pay the tax for adding the cast code for every call on those types. What I don't understand is how you're getting the types of the |
We use JavaScript's The list is different because the browser creates a list whose constructor name is simply Hope this helps clarify things a bit. |
Makes sense, thx again |
…her than `as` dynamic assert See dart-lang/sdk#39627 . Fix `as` assertion causes a runtime exception if value types do no match. cast<T> generic type reify made more concrete creates a new iterable List
…her than `as` dynamic assert See dart-lang/sdk#39627 . Fix `as` assertion causes a runtime exception if value types do no match. cast<T> generic type reify made more concrete creates a new iterable List
Change-Id: I51d6f6759520fb5703065f9b223a65e47bed56f4 Bug: #39627 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/219560 Auto-Submit: Riley Porter <[email protected]> Commit-Queue: Riley Porter <[email protected]> Reviewed-by: Sigmund Cherem <[email protected]>
Change-Id: I51d6f6759520fb5703065f9b223a65e47bed56f4 Bug: #39627 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/219560 Auto-Submit: Riley Porter <[email protected]> Commit-Queue: Riley Porter <[email protected]> Reviewed-by: Sigmund Cherem <[email protected]>
Dart version 2.7.0 (build 2.7.0-dev.2.1 e4344a5) -- included with Flutter (Win 10 x64)
This issue also occurs in current dev branch of Flutter, which uses Dart 2.7.0 (not sure if "2.7.0" alone means "2.7.0-dev0.0").
If I try this:
Then I get this (in Chrome):
Full stack:
The text was updated successfully, but these errors were encountered: