dint-dev / universal_html

Cross-platform 'dart:html'.
https://pub.dev/packages/universal_html
Apache License 2.0
201 stars 60 forks source link

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

Open Ruud14 opened 9 months ago

Ruud14 commented 9 months ago

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.

`

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 commented 9 months ago

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.