Skip to content

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

Open
@jake-johnson-disney

Description

@jake-johnson-disney

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-web-jsIssues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop.library-htmlweb-librariesIssues impacting dart:html, etc., libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions