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

Method 'close' cannot be called on 'WindowBase?' because it is potentially null. #91

Open
Ruud14 opened this issue Nov 25, 2023 · 1 comment

Comments

@Ruud14
Copy link

Ruud14 commented Nov 25, 2023

The following code works correctly on web:

var other = html.window.open(...);
other.close();

I obviously have this code behind a kIsWeb, such that this code is never run on Android or iOS.
However, when I try to build my project for android I get the following error:

`
Error: Method 'close' cannot be called on 'WindowBase?' because it is potentially null.

  • 'WindowBase' is from 'package:universal_html/src/html.dart' ('../../../AppData/Local/Pub/Cache/hosted/pub.dev/universal_html-2.2.4/lib/src/html.dart').
    Try calling using ?. instead.
    other.close();
    ^^^^^

`

As the error states, this can be fixed by changing the code to other?.close();, however my VScode complains about this stating that "The receiver can't be null, so the null-aware operator '?.' is unnecessary."

Thus it seems like the return value of .open is not nullable on web, but nullable on other platforms.

@fsw
Copy link
Collaborator

fsw commented Nov 25, 2023

Such tiny interface differences were incorporated when adding null safety. And were not noticed for functions that are not very usable on io side (like window.open)

universal_html interfaces should be consistent with those of dart:html

You can confirm that dart interface for version we support 2.17.0 returns WindowBase (non nullable)

https://api.dart.dev/stable/2.17.0/dart-html/Window/open.html

So I think it would be best to change return type in this function:

https://github.com/dint-dev/universal_html/blob/master/lib/src/html/api/window.dart

And make it throw UnimplementedException instead of returning null.

Feel free to submit a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants