【问题标题】:Current location with flutter颤振的当前位置
【发布时间】:2020-11-20 10:39:46
【问题描述】:

我在检索当前位置时遇到问题。

第 1 步: 当我按下按钮时,它将打开启用位置服务的默认弹出窗口,因为我检查它没有启用位置。

第 2 步: 如果我手动启用该位置,我将获得当前位置,如果我执行第一步它不起作用

       final Location location = Location();

  LocationData _location;
  String _error;

  Future<void> _getLocation() async {
    setState(() {
      _error = null;
    });
    try {
      final LocationData _locationResult = await location.getLocation();
      setState(() {
        _location = _locationResult;
      });
    } on PlatformException catch (err) {
      setState(() {
        _error = err.code;
      });
    }
  }
 

【问题讨论】:

  • 建议你使用定位包pub.dev/packages/location
  • 如果我手动启用,我将获得位置,但如果我执行第 1 步,它不起作用你知道是什么原因吗?或者。可以分享一些示例代码@ByteMe
  • 我一直在尝试使用您建议的位置插件,但它不起作用,https://github.com/Lyokone/flutterlocation/blob/master/location/example/lib/get_location.dart,我正在尝试使用他们的 github。但它会抛出这个错误。2 postional argument expected but 0 found@字节我

标签: flutter dart flutter-dependencies


【解决方案1】:

这是一个如何使用位置包获取位置的示例

Future _getLocation() async {
    Location location = new Location();
    LocationData _pos = await location.getLocation();
    SharedPrefrence().setLatitude(_pos.latitude);
    SharedPrefrence().setLongitude(_pos.longitude);
}

了解更多关于设置包here

【讨论】:

  • 我确实尝试过使用它,但是当我 Location location = new Location(); 并导入包然后 Location location = new Location(); 在这一行中它显示像这样的错误 2 postional argument expected but 0 found @ByteMe
  • 嗯,你能用你试过的代码更新你的问题吗
  • @Morbius 你能停止你的应用程序并执行flutter clean &amp;&amp; flutter run 然后检查吗?保持代码不变Location location = new Location()
  • 我已经这样做了,但它不允许我运行应用程序@Alok
  • 别担心@Morbius,让我们做一些事情让它发挥作用。这是现在工作还是你已经离开了一段时间?您能帮我解决运行应用程序时遇到的错误吗?
【解决方案2】:

正如我检查的那样,问题出在两个名为

的插件上

flutter_google_places: ^0.2.6

geocoder: ^0.2.1

在页面中,Location中的Location location = new Location()是两个插件使用的,所以插件是相互关联的。

因为我有两个按钮,一个用于获取当前位置,一个用于搜索自定义位置。

所以我所做的是在导入时我必须添加一个前缀并使用一些函数以及前缀名称来搜索位置函数,现在它工作得很好。 现在它正在请求许可并显示对话框在不离开应用程序的情况下启用位置

感谢@Alok 和@ByteMe 帮助我

import 'package:flutter_google_places/flutter_google_places.dart' as google_place;
import 'package:google_maps_webservice/places.dart' as map_service;
import 'package:location/location.dart';
    
//Current Location Function

      Future _getLocation() async {
        Location location = new Location();
        LocationData _currentPosition = await location.getLocation();
        SharedPrefrence().setLatitude(_currentPosition.latitude.toString());
        SharedPrefrence().setLongitude(_currentPosition.longitude.toString());
       
        Future loginstatus = SharedPrefrence().getLogedIn();
      
          loginstatus.then((data) {
            if (data == true) {
              Navigator.pop(context, true);
              Navigator.pushAndRemoveUntil(
                  context,
                  MaterialPageRoute(builder: (context) => HomeScreen()),
                  ModalRoute.withName("/login"));
            } else {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => LoginScreen(),
                ),
              );
            }
          });
        
      }

【讨论】:

  • 恭喜莫比乌斯。我投票支持你的 effrots :)
  • 谢谢,如果你没有告诉我将解决方案作为单独的项目尝试,我不会知道?,@Alok
  • 这就是我们来到莫比乌斯的原因。欢迎来到真正的开发者生活 :) 一切顺利
  • ???✌✌@Alok @ByteMe
  • 如果您喜欢他的回答,您可以投票 ByteMe 的回答。稍后您可以将您的答案标记为正确。我猜 24 小时后,您将被允许这样做
猜你喜欢
  • 2021-04-21
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-12
  • 1970-01-01
  • 2018-10-18
  • 2020-06-13
  • 2020-11-02
相关资源
最近更新 更多