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

Unexpected 'null' in non-nullable properties of the Navigator class #326

Open
miquelbeltran opened this issue Dec 17, 2024 · 1 comment
Open

Comments

@miquelbeltran
Copy link

Hello, we are getting reports of this issue from device_info_plus users:

[Log] TypeError: null: type 'Null' is not a subtype of type 'double' (dart_sdk.js, line 38043)

The deviceMemory is declared as double but sometimes it may be null.

The following comment has more details: fluttercommunity/plus_plugins#3391 (comment)

I am not sure if this may be the case for other navigator properties.

@srujzs
Copy link
Contributor

srujzs commented Dec 17, 2024

deviceMemory is unavailable on Firefox and Safari: https://developer.mozilla.org/en-US/docs/Web/API/Navigator/deviceMemory.

In general, we use the IDL as the source of truth. Making a property nullable to tackle various browser capabilities and edge-cases means excessive null-checks for users and even more breaking changes when those eventually change. dart:html's body member is one such example of leading to excessive null-checks. We have discussed emitting metadata though on various members to signal which browser supports it, but that may be too verbose.

As for the fix, I'd avoid catching cast errors, and instead use extension members:

extension on web.Navigator {
  @JS('deviceMemory')
  external double? get browserSafeDeviceMemory;
}

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

No branches or pull requests

2 participants