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

When geolocation api is disabled, app fails to load in Firefox #54975

Open
jake-johnson-disney opened this issue Feb 21, 2024 · 2 comments
Open
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. library-html web-libraries Issues impacting dart:html, etc., libraries

Comments

@jake-johnson-disney
Copy link

There's already an issue here: #30313 that was closed a long time ago, but the issue still exists.

In Firefox, it is possible for the user to completely disable the Geolocation API in about:config by toggling geo.enabled. In this case, window.navigator.geolocation will be undefined.

The application fails to fully load if trying to use geolocation anywhere with something like Uncaught : Null check operator used on a null value

Here's a simple example that reproduces the issue:

import 'dart:html' as html;

import 'package:flutter/material.dart';

void main() => runApp(
      MaterialApp(
        home: Material(
          child: Center(
            child: FutureBuilder<html.Geoposition?>(
              future: HtmlGeolocationManager().getCurrentPosition(),
              builder: (BuildContext context,
                  AsyncSnapshot<html.Geoposition?> snapshot) {
                if (snapshot.hasData) {
                  return Text(snapshot.data?.coords?.latitude.toString() ?? '');
                } else {
                  return const Text('getting location...');
                }
              },
            ),
          ),
        ),
      ),
    );

class HtmlGeolocationManager {
  final html.Geolocation? _geolocation;

  HtmlGeolocationManager() : _geolocation = html.window.navigator.geolocation;

  Future<html.Geoposition?> getCurrentPosition({
    bool? enableHighAccuracy,
    Duration? timeout,
  }) async {
    try {
      html.Geoposition? geoPosition;
      if (_geolocation != null) {
        geoPosition = await _geolocation.getCurrentPosition(
          enableHighAccuracy: enableHighAccuracy,
          timeout: timeout,
        );
      }

      return geoPosition;
    } on html.PositionError catch (e) {
      throw e;
    }
  }
}

I'd be happy to contribute a fix, if someone can help point me in the right direction

@jake-johnson-disney
Copy link
Author

dart info:

❯ dart info      

If providing this information as part of reporting a bug, please review the information
below to ensure it only contains things you're comfortable posting publicly.

#### General info

- Dart 3.2.6 (stable) (Wed Jan 24 13:41:58 2024 +0000) on "macos_arm64"
- on macos / Version 14.2.1 (Build 23C71)
- locale is en-US

#### Project info

- sdk constraint: '>=3.2.6 <4.0.0'
- dependencies: cupertino_icons, flutter
- dev_dependencies: flutter_lints, flutter_test

#### Process info

| Memory |  CPU | Elapsed time | Command line                                                                               |
| -----: | ---: | -----------: | ------------------------------------------------------------------------------------------ |
|   4 MB | 0.0% |     18:24:25 | dart devtools --machine --allow-embedding                                                  |
|   8 MB | 0.0% |  07-00:09:27 | dart language-server --client-id=IntelliJ-IDEA --client-version=IU-223.8836.41 --protocol=analyzer |
| 670 MB | 0.0% |        46:29 | dart language-server --client-id=IntelliJ-IDEA --client-version=IU-223.8836.41 --protocol=analyzer |
|  13 MB | 0.0% |     18:24:25 | dart language-server --protocol=lsp --client-id=VS-Code --client-version=3.82.0            |
|  17 MB | 0.0% |  07-00:09:27 | flutter_tools.snapshot daemon                                                              |
|  27 MB | 0.4% |        46:30 | flutter_tools.snapshot daemon                                                              |
|  20 MB | 0.0% |     18:24:25 | flutter_tools.snapshot daemon                                                              |

@keertip keertip added the area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. label Feb 21, 2024
@nshahan nshahan added the web-libraries Issues impacting dart:html, etc., libraries label Feb 22, 2024
@srujzs
Copy link
Contributor

srujzs commented Feb 28, 2024

Thanks for filing and the repro!

I can't seem to quite reproduce the same issue with Firefox (I come across the rethrow instead even with the about:config changes, but it may be a user error), but I'm guessing that the culprit is the getting of geolocation. With Dart 3, native APIs are null-checked if their return type is non-nullable. This was not always the case in Dart 2.

An alternative is declaring a nullable version of geolocation using JS interop since it's a native API:

extension on Navigator {
  // Rename so you don't come across collisions with `Navigator.geolocation`.
  @JS('geolocation')
  external Geolocation? get geolocation_;
}

and using geolocation_ instead. Let me know if this works as a workaround.

There's a general theme of how to support compatibility with dart:html. In the past, we've tried to incorporate all the possible instances where an API might not exist, but this led to many nullable APIs even when they're not null in most cases. It also was not exhaustive (as you can see), and so may miss instances when an API could be unavailable.

As an aside, we're move to package:web going forward and investing effort there to move away from some of the complications of dart:html to something that is leaner and maps directly to the web. If you find that it's easier to use for your project, consider trying it out.

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. library-html web-libraries Issues impacting dart:html, etc., libraries
Projects
None yet
Development

No branches or pull requests

5 participants