【问题标题】:How to wait until user permissions, to get location coordinate in Geolocator to load a Google map?如何等到用户权限,才能在 Geolocator 中获取位置坐标以加载 Google 地图?
【发布时间】:2019-12-27 03:35:48
【问题描述】:

我正在尝试通过获取当前用户位置来加载谷歌地图。

当我第一次注册 (firebase) 时,它会要求位置许可,那么只会出现这个异常。 (登录时没有这个,第一次注册后)

这是我在 pubspec.yaml 中的地理定位器和位置插件

geolocator: ^4.0.2 version
location: ^2.0.0

我该如何解决这个问题?

【问题讨论】:

    标签: android firebase flutter


    【解决方案1】:

    正如官方示例所说,您应该先检查权限。像这样

      Widget build(BuildContext context) {
       return FutureBuilder<GeolocationStatus>(
        future: Geolocator().checkGeolocationPermissionStatus(),
        builder:
            (BuildContext context, AsyncSnapshot<GeolocationStatus> snapshot) {
          if (!snapshot.hasData) {
            return const Center(child: CircularProgressIndicator());
          }
    
          if (snapshot.data == GeolocationStatus.denied) {
            return const PlaceholderWidget('Access to location denied',
                'Allow access to the location services for this App using the device settings.');
          }
    
          return Center(.... your code)
    

    你可以在这里找到完整的例子https://github.com/Baseflow/flutter-geolocator/tree/develop/example

    【讨论】:

      猜你喜欢
      • 2021-01-04
      • 2021-08-26
      • 1970-01-01
      • 2015-03-28
      • 1970-01-01
      • 1970-01-01
      • 2016-07-26
      • 2017-12-06
      • 1970-01-01
      相关资源
      最近更新 更多