flutter-web:通过“位置"插件获取网络上的位置 [英] flutter-web: get location on web by Location plugin

查看:89
本文介绍了flutter-web:通过“位置"插件获取网络上的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个老的flutter项目,我向其中添加了Web支持,现在,我试图在Flutter Web中获取我的位置,所以我添加了

什么都没发生!在vsCode控制台中,我只是收到此错误:

 错误:[object GeolocationPositionError]在Object.createErrorWithStack(http://localhost:43705/dart_sdk.js:4351:12)在Object._rethrow(http://localhost:43705/dart_sdk.js:37962:16)在async._AsyncCallbackEntry.new.callback(http://localhost:43705/dart_sdk.js:37956:13)在Object._microtaskLoop(http://localhost:43705/dart_sdk.js:37788:13)在_startMicrotaskLoop(http://localhost:43705/dart_sdk.js:37794:13)在http://localhost:43705/dart_sdk.js:33303:9 

**使用@JS('navigator.geolocation')

我也尝试,但从未成功< 方法被调用,没有任何后果.

解决方案

Dart当前与Geolocation API有关.考虑编写互操作库或使用我的库.下面的链接地理位置PolyFill

I have old flutter project, I added web support to it, Now ,I am trying to get my Location in flutter web , So i added location to my page. this is my code:

  @override
  void initState() {
    super.initState();
    _getLocation();
  }

  Future _getLocation() async {
    Location location = new Location();

    var _permissionGranted = await location.hasPermission();
    _serviceEnabled = await location.serviceEnabled();

    if (_permissionGranted != PermissionStatus.granted || !_serviceEnabled) {
      _permissionGranted = await location.requestPermission();
      _serviceEnabled = await location.requestService();
    } else {
      print("-----> $_serviceEnabled");

      setState(() {
        _serviceEnabled = true;
        _loading = false;
      });
    }

    try {
      final LocationData currentPosition = await location.getLocation();
      setState(() {
        longitude = currentPosition.longitude.toString();
        latitude = currentPosition.latitude.toString();
        print(
            '${widget.url}?BranchName=&latitude=${latitude}&longitude=${longitude}');
        _loading = false;
      });
    } on PlatformException catch (err) {
      _loading = false;
      print("-----> ${err.code}");
    }
  }

After getting Location permission by chrome,

Nothing happening! In vsCode console i just got this error:

Error: [object GeolocationPositionError]


    at Object.createErrorWithStack (http://localhost:43705/dart_sdk.js:4351:12)
    at Object._rethrow (http://localhost:43705/dart_sdk.js:37962:16)
    at async._AsyncCallbackEntry.new.callback (http://localhost:43705/dart_sdk.js:37956:13)
    at Object._microtaskLoop (http://localhost:43705/dart_sdk.js:37788:13)
    at _startMicrotaskLoop (http://localhost:43705/dart_sdk.js:37794:13)
    at http://localhost:43705/dart_sdk.js:33303:9

**USING @JS('navigator.geolocation')

I also try this, but never success method called and nothing heppen.

解决方案

Dart currently has an issue with the Geolocation API. Consider writing an interop library or using mine. Link below Geolocation PolyFill

这篇关于flutter-web:通过“位置"插件获取网络上的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆