Flutter - 谷歌地图不会等待定位

El *_*bre 3 google-maps flutter

我正在尝试显示当前位置的地图。为此,我使用了位置谷歌地图插件(最新版本)。

我有一个类似的代码:

var lng, lat;

          @override
          initState() {
            super.initState();
            loading = true;
            getLocation();
          }

              Future getLocation() async {
                    final location = Location();
                    var currentLocation = await location.getLocation();
                    setState(() {
                      lat = currentLocation.latitude;
                      lng = currentLocation.longitude;
                      loading=false;
                    });
                  }

             loading==false ? GoogleMap(
                 mapType: MapType.hybrid,
                 myLocationButtonEnabled: true,
                 myLocationEnabled: true,
                 initialCameraPosition: CameraPosition(
                      target: LatLng(lat, lng),
                      zoom: 15.0,
                            )):null,
Run Code Online (Sandbox Code Playgroud)

当我们使用地图查看视图时,会在 1 秒左右出现错误(然后正确加载视图),并在控制台中显示此错误

我/扑(15567):???小部件库捕获的异常 ????????????????????????????????????????????? ??????????????? I/flutter (15567): 以下断言被抛出构建 HomePageScreen(dirty, dependencies: I/flutter (15567): [_LocalizationsScope-[GlobalKey#78c30], MediaQuery], state: _HomePageScreen#9c9d2): I/flutter (15567) ): 'package:google_maps_flutter/src/location.dart': 失败的断言: line 17 pos 16: 'latitude != I/flutter (15567): null': 不是真的。

我调试它,错误相对简单:位置插件加载纬度和经度慢,谷歌地图插件加载更快。所以我们有一个错误。

问题是:如何强制谷歌地图等待位置插件的位置和经度?

Mic*_*ono 9

Container()当您的latlng为空时,显示一个空的或任何加载指示器。

lat == null || lng == null
  ? Container()
  : GoogleMap(
      mapType: MapType.hybrid,
      myLocationButtonEnabled: true,
      myLocationEnabled: true,
      initialCameraPosition: CameraPosition(
        target: LatLng(lat, lng),
        zoom: 15.0,
      ),
    );
Run Code Online (Sandbox Code Playgroud)