universal_html icon indicating copy to clipboard operation
universal_html copied to clipboard

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

Open Ruud14 opened this issue 2 years ago • 1 comments

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.

Ruud14 avatar Nov 25 '23 14:11 Ruud14

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.

fsw avatar Nov 25 '23 15:11 fsw