Skip to content
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

Wrong return type for getInstalledRelatedApps. #56986

Closed
gmpassos opened this issue Oct 29, 2024 · 3 comments
Closed

Wrong return type for getInstalledRelatedApps. #56986

gmpassos opened this issue Oct 29, 2024 · 3 comments
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-libraries Issues impacting dart:html, etc., libraries

Comments

@gmpassos
Copy link
Contributor

window.navigator.getInstalledRelatedApps() should return a List< RelatedApplication > and not a RelatedApplication.

The current return signature is generating an error:

errors.dart:296 Uncaught (in promise) DartError: TypeError: Instance of 'JSArray<dynamic>': type 'List<dynamic>' is not a subtype of type 'FutureOr<RelatedApplication>?'
    at Object.throw_ [as throw] (errors.dart:296:3)
    at Object._failedAsCheck (rti.dart:1405:3)
    at dart_rti.Rti.new._generalNullableAsCheckImplementation (rti.dart:1395:3)
    at dart_rti.Rti.new._installSpecializedAsCheck (rti.dart:1278:30)
    at js_util_patch.dart:483:58

Declaration:
https://github.com/dart-lang/sdk/blob/0b38e4b189d1358446f883d68e69cf7d81d6e0ac/sdk/lib/html/dart2js/html_dart2js.dart#L22795

Web API reference:
https://developer.mozilla.org/en-US/docs/Web/API/Navigator/getInstalledRelatedApps

@dart-github-bot
Copy link
Collaborator

Summary: The getInstalledRelatedApps() method in the Dart html library incorrectly returns a single RelatedApplication object instead of a List<RelatedApplication>. This mismatch causes type errors when attempting to use the returned value.

@dart-github-bot dart-github-bot added area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Oct 29, 2024
@biggs0125 biggs0125 added the web-libraries Issues impacting dart:html, etc., libraries label Oct 30, 2024
@a-siva a-siva removed the triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. label Oct 30, 2024
@biggs0125
Copy link

It does appear that the typing in dart:html is wrong for this API. However, we’re working on putting dart:html into deprecation mode which means we’re stopping fixes for these sorts of type errors, especially for experimental APIs.

We recommend switching over to package:web if it contains the API you need, or using dart:js_interop otherwise. In this case, since getInstalledRelatedApps is an experimental API it won’t be included in package:web. This means you’ll have to invoke it a little more manually:


import 'dart:js_interop';

extension type RelatedApplication(JSObject _) implements JSObject {
  external String? id;

  external String? platform;

  external String? url;
}

@JS('window.navigator.getInstalledRelatedApps')
external JSPromise<JSArray<RelatedApplication>> _getInstalledRelatedApps();

Future<List<RelatedApplication>> getInstalledRelatedApps() => _getInstalledRelatedApps().toDart.then((e) => e.toDart));

Then you can invoke the method like await (getInstalledRelatedApps().toDart).

For more information on the new JS interop, see our docs.

@biggs0125 biggs0125 closed this as not planned Won't fix, can't repro, duplicate, stale Oct 30, 2024
@gmpassos
Copy link
Contributor Author

Thanks for the dart:js_interop example.

For more information on the new JS interop, see our docs.

I think this kind of example should be included in the dart:js_interop documentation.

The Dart team has invested a lot of effort into dart:js_interop, especially for Wasm compatibility. Since the future will likely rely on this new approach to interacting with JavaScript, good documentation and examples are essential to guide people’s efforts in the right direction.

Best regards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-libraries Issues impacting dart:html, etc., libraries
Projects
None yet
Development

No branches or pull requests

4 participants