我有一个地图应用程序,它使用google_map_flutter包并显示全屏主题地图。我的困惑是,当我构建应用程序时,我收到 setMapStyle 的未处理异常,即使地图与主题一起显示。
未处理的异常:NoSuchMethod错误:方法"setMapStyle"是 调用空。E/颤振 (30877(: 接收器:空 E/颤振 (30877(: 尝试调用: setMapStyle("[\r {\r \"featureType\": \"landscape\",\r \"elementType\": \"geometry\",\r.......
主题是一个 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);
}
}
}
无法分配参数类型"完成者" 到参数类型"GoogleMapController"。
[/编辑]
谢谢
你需要初始化Completer
类,在你的State
类下写下写下以下内容:
Completer<GoogleMapController> _controller = Completer();
然后在调用setMapStyle
时使用变量_controller
:
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);
}
使用await
标记Future
方法以进行异步处理非常重要。
在_onMapCreated
中,您可以添加加载 mapStyle 的 future 方法。
void _onMapCreated(GoogleMapController controller) async {
final mapStyle = await rootBundle.loadString(
'assets/themes/map/day/simple_bright.json',
);
await controller.setMapStyle(mapStyle);
_updateStatus = await PermissionHandler()
.checkPermissionStatus(
PermissionGroup.locationWhenInUse
);
}