颤动的当前位置

Current location with flutter

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

第 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;
      });
    }
  }
 

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

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

了解有关设置包的更多信息here

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

的插件上

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(),
                ),
              );
            }
          });
        
      }