0

我有一个使用 google_map_flutter 包并显示全屏主题地图的地图应用程序。我的困惑是,当我构建应用程序时,我收到一个未处理的 setMapStyle 异常,即使地图显示为主题。

未处理的异常:NoSuchMethodError:在 null 上调用了方法“setMapStyle”。E/flutter (30877): 接收者: null E/flutter (30877): 尝试调用: setMapStyle("[\r\n {\r\n \"featureType\": \"landscape\",\r\n \ "elementType\": \"几何\",\r\n.......

主题是一个 json 文件,我使用下面的 initState 中的代码加载它。

@override
  void initState() {
    super.initState();
    // Show the campus Map
    getSunData();
    // _showCampusMap();
    WidgetsBinding.instance.addObserver(this);
    // Check location permission has been granted

    PermissionHandler()
        .checkPermissionStatus(PermissionGroup
            .locationWhenInUse) //check permission returns a Future
        .then(_updateStatus); // handling in callback to prevent blocking UI

    rootBundle
        .loadString('assets/themes/map/day/simple_bright.json')
        .then((string) {
      mapStyle = string;
    });

    getUserLocation();
  }

我设置样式的方法在这里。

// method that is called on map creation and takes a MapController as a parameter
  void _onMapCreated(GoogleMapController controller) async {
    PermissionHandler()
        .checkPermissionStatus(PermissionGroup
            .locationWhenInUse) //check permission returns a Future
        .then(_updateStatus); // handling in callback to prevent blocking UI

    controller.setMapStyle(mapStyle);
  }

这是我的谷歌地图代码

_userLocation == null
                  ? Center(
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: <Widget>[
                          CircularProgressIndicator(
                            backgroundColor: Theme.UniColour.primary[900],
                          ),
                          SizedBox(height: 20.0),
                          Text("Retrieving your location..."),
                        ],
                      ),
                    )
                  : GoogleMap(
                      onMapCreated: _onMapCreated,
                      initialCameraPosition: // required parameter that sets the starting camera position. Camera position describes which part of the world you want the map to point at.
                          CameraPosition(
                              target: _userLocation,
                              zoom: _defaultZoom,
                              tilt: _tiltAngle), //LatLng(53.467125, -2.233966)
                      scrollGesturesEnabled: _scrollGesturesEnabled,
                      tiltGesturesEnabled: _tiltGesturesEnabled,
                      compassEnabled: _compassEnabled,
                      rotateGesturesEnabled: _rotateGesturesEnabled,
                      myLocationEnabled: _myLocationEnabled,
                      buildingsEnabled: _buildingsEnabled, // not added to db
                      indoorViewEnabled: _indoorViewEnabled, // not added to db
                      mapToolbarEnabled: _mapToolbarEnabled, // not added to db
                      myLocationButtonEnabled:
                          _myLocationButtonEnabled, // not added to db
                      mapType: _currentMapType,
                      zoomGesturesEnabled: _zoomGesturesEnabled,
                      cameraTargetBounds: CameraTargetBounds(
                        new LatLngBounds(
                          northeast: uniCampusNE,
                          southwest: uniCampusSW,
                        ),
                      ),
                      minMaxZoomPreference:
                          MinMaxZoomPreference(_minZoom, _maxZoom),
                    ),

任何想法为什么会发生此异常,是否需要修复以及我将如何做到这一点?

[编辑]

void _updateStatus(PermissionStatus status) {
    if (status != _status) {
      // check status has changed
      setState(() {
        _status = status; // update
        _onMapCreated(controller);
      });
    } else {
      if (status != PermissionStatus.granted) {
        //print("REQUESTING PERMISSION");
        PermissionHandler().requestPermissions(
            [PermissionGroup.locationWhenInUse]).then(_onStatusRequested);
      }
    }
  }

参数类型“Completer”不能分配给参数类型“GoogleMapController”。

[/编辑]

谢谢

4

1 回答 1

0

您需要初始化Completer类,在您的State类下写下以下内容:

 Completer<GoogleMapController> _controller = Completer();

_controller然后在调用时使用变量setMapStyle

  void _onMapCreated(GoogleMapController controller) async {
    PermissionHandler()
        .checkPermissionStatus(PermissionGroup
            .locationWhenInUse) //check permission returns a Future
        .then(_updateStatus); // handling in callback to prevent blocking UI

   _controller.setMapStyle(mapStyle);
  }
于 2020-01-29T10:27:15.537 回答