我已经成功地在我的flutter应用程序上放置了谷歌地图,但每当位置更新触发时,我的控制台上就会出现错误。地图仍然在更新,一切都很好,但我想完全理解为什么我会出现这个错误,以及如何修复它。
以下是我的完整代码和我在控制台上得到的错误
错误
E/flutter (12290): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: MissingPluginException(No implementation found for method camera#animate on channel plugins.flutter.io/google_maps_0)
E/flutter (12290): #0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:157:7)
E/flutter (12290): <asynchronous suspension>
E/flutter (12290): #1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:332:12)
E/flutter (12290): #2 MethodChannelGoogleMapsFlutter.animateCamera (package:google_maps_flutter_platform_interface/src/method_channel/method_channel_google_maps_flutter.dart:294:10)
E/flutter (12290): #3 GoogleMapController.animateCamera (package:google_maps_flutter/src/controller.dart:160:39)
E/flutter (12290): #4 HomeState.getLocation.<anonymous closure> (package:harpy/activities/Home.activity.dart:45:20)
E/flutter (12290): #5 _rootRunUnary (dart:async/zone.dart:1198:47)
E/flutter (12290): #6 _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter (12290): #7 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter (12290): #8 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:357:11)
E/flutter (12290): #9 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:285:7)
E/flutter (12290): #10 _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:127:11)
E/flutter (12290): #11 _MapStream._handleData (dart:async/stream_pipe.dart:224:10)
E/flutter (12290): #12 _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:157:13)
E/flutter (12290): #13 _rootRunUnary (dart:async/zone.dart:1198:47)
E/flutter (12290): #14 _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter (12290): #15 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter (12290): #16 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:357:11)
E/flutter (12290): #17 _DelayedData.perform (dart:async/stream_impl.dart:611:14)
E/flutter (12290): #18 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:730:11)
E/flutter (12290): #19 _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:687:7)
E/flutter (12290): #20 _rootRun (dart:async/zone.dart:1182:47)
E/flutter (12290): #21 _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (12290): #22 _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter (12290): #23 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter (12290): #24 _rootRun (dart:async/zone.dart:1190:13)
E/flutter (12290): #25 _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (12290): #26 _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter (12290): #27 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter (12290): #28 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter (12290): #29 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
E/flutter (12290):
Restarted application in 4,395ms.
全代码
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:harpy/widgets/BottomSheet.widget.dart';
import 'package:harpy/widgets/Navigation.widget.dart';
import 'package:location/location.dart';
class Home extends StatefulWidget {
@override
State<Home> createState() => HomeState();
}
class HomeState extends State<Home> {
BitmapDescriptor userLocationIcon;
GoogleMapController mapController;
Completer<GoogleMapController> _controller = Completer();
Location _location = Location();
LocationData _locationData;
Map<MarkerId, Marker> markers = <MarkerId, Marker>{};
Set<Marker> _markers = {};
bool expand = false;
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
getLocation() async{
mapController = await _controller.future;
_location.onLocationChanged.listen((LocationData currentLocation) {
LatLng latLng = LatLng(currentLocation.latitude, currentLocation.longitude);
_markers.add(Marker(
markerId: MarkerId("myLocation"),
position: latLng,
icon: userLocationIcon,
rotation: currentLocation.heading)
);
CameraUpdate cameraUpdate = CameraUpdate.newLatLngZoom(latLng, 17.4);
mapController.animateCamera(cameraUpdate);
setStateIfMounted(() {
_locationData = currentLocation;
});
});
}
void setStateIfMounted(f) {
if (mounted) setState(f);
}
onExpand(){
setState(() { expand = !expand; });
}
Future<bool> _onWillPop() async {
if(expand){
setState(() { expand = false; });
return false;
}
return true;
}
createMarkerImages(){
BitmapDescriptor.fromAssetImage(
ImageConfiguration(devicePixelRatio: 2.5), 'assets/images/car.png').then((onValue) {
userLocationIcon = onValue;
});
}
@override
void initState() {
super.initState();
createMarkerImages();
getLocation();
}
@override
void dispose() {
super.dispose();
mapController.dispose();
}
@override
Widget build(BuildContext context) {
Size _size = MediaQuery.of(context).size;
return WillPopScope(
onWillPop: _onWillPop,
child: SafeArea(
child: Scaffold(
key: _scaffoldKey,
drawer: Navigation(),
body: Stack(
children: <Widget>[
Container(
height: _size.height - (_size.height / 3),
child: GoogleMap(
markers: _markers,
myLocationEnabled: true,
zoomControlsEnabled: false,
myLocationButtonEnabled: false,
mapType: MapType.normal,
initialCameraPosition: CameraPosition(
target: LatLng(this._locationData?.latitude ?? 6.7008168, this._locationData?.longitude ?? -1.6998494),
zoom: 14.4746,
),
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
}
),
),
Positioned(
top: 20,
left: 20,
child: Container(
height: 45,
width: 45,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(25),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.3),
spreadRadius: 3,
blurRadius: 10,
offset: Offset(0, 1)
)
]
),
child: IconButton(
icon: Icon(Icons.menu),
onPressed: ()=> _scaffoldKey.currentState.openDrawer(),
),
),
),
CustomBottomSheet(onPressed: onExpand, expand: expand)
],
),
),
),
);
}
}
我创建了一个streamsubscription变量;
StreamSubscription<LocationData> streamHandler;
将其引用到订阅并在的处置方法中取消
@override
void dispose() {
super.dispose();
mapController.dispose();
streamHandler.cancel();
}
我再也看不到错误了:(。
转到文件->使缓存/重新启动无效。
那就行了。