MissingPluginException(MissingPluginException(在通道 plugins.flutter.io/google_maps_53 上找不到方法 camera#an

Posted

技术标签:

【中文标题】MissingPluginException(MissingPluginException(在通道 plugins.flutter.io/google_maps_53 上找不到方法 camera#animate 的实现))【英文标题】:MissingPluginException (MissingPluginException(No implementation found for method camera#animate on channel plugins.flutter.io/google_maps_53)) 【发布时间】:2020-10-20 08:25:10 【问题描述】:

我最近在我的 Flutter 应用中安装了谷歌地图,但我不断收到此错误,

这发生在我调用的每个地图控制器上,即使 oncreated 方法也会给我同样的问题,我首先尝试了 flutter clean 和 flutter pub get after 但无济于事,现在我不知道

这是我创建地图的主要 dart 文件

GoogleMap(
   mapToolbarEnabled: false,
   mapType: MapType.normal,
   initialCameraPosition: CameraPosition(target:appState.initialPosition, zoom: 15.0),
   markers: appState.markers,
   onCameraMove: appState.onCameraMove,
   polylines: appState.polyLines,
   myLocationEnabled: true,
   zoomControlsEnabled: false,
   onMapCreated: appState.onCreated,compassEnabled: false,
   myLocationButtonEnabled: false,   
),

这是我单独的 appstate.dart 文件

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:toladriver/requests/google_maps_requests.dart';


class AppState with ChangeNotifier 
  static LatLng _initialPosition;
  LatLng _lastPosition = _initialPosition;
  bool locationServiceActive = true;
  final Set<Marker> _markers = ;
  final Set<Polyline> _polyLines = ;
  GoogleMapController _mapController;
  GoogleMapsServices _googleMapsServices = GoogleMapsServices();
  TextEditingController locationController = TextEditingController();
  TextEditingController destinationController = TextEditingController();
  LatLng get initialPosition => _initialPosition;
  LatLng get lastPosition => _lastPosition;
  GoogleMapsServices get googleMapsServices => _googleMapsServices;
  GoogleMapController get mapController => _mapController;
  Set<Marker> get markers => _markers;
  Set<Polyline> get polyLines => _polyLines;

  AppState() 
    _getUserLocation();
    _loadingInitialPosition();
  
// ! TO GET THE USERS LOCATION
  void _getUserLocation() async 
    print("GET USER METHOD RUNNING =========");
    Position position = await Geolocator()
        .getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    List<Placemark> placemark = await Geolocator()
        .placemarkFromCoordinates(position.latitude, position.longitude);
    _initialPosition = LatLng(position.latitude, position.longitude);
    print("the latitude is: $position.longitude and th longitude is: $position.longitude ");
    print("initial position is : $_initialPosition.toString()");
    locationController.text = placemark[0].name;
    notifyListeners();
  

  // ! TO CREATE ROUTE
  void createRoute(String encondedPoly) 
    _polyLines.add(Polyline(
        polylineId: PolylineId(_lastPosition.toString()),
        width: 5,
        points: _convertToLatLng(_decodePoly(encondedPoly)),
        color: Colors.blueGrey));
    notifyListeners();
  

  // ! ADD A MARKER ON THE MAO
  void _addMarker(LatLng location, String address) 
    _markers.add(Marker(
        markerId: MarkerId(_lastPosition.toString()),
        position: location,
        infoWindow: InfoWindow(title: address, snippet: "go here"),
        icon: BitmapDescriptor.defaultMarker));
    notifyListeners();
  

  // ! CREATE LAGLNG LIST
  List<LatLng> _convertToLatLng(List points) 
    List<LatLng> result = <LatLng>[];
    for (int i = 0; i < points.length; i++) 
      if (i % 2 != 0) 
        result.add(LatLng(points[i - 1], points[i]));
      
    
    return result;
  

  // !DECODE POLY
  List _decodePoly(String poly) 
    var list = poly.codeUnits;
    var lList = new List();
    int index = 0;
    int len = poly.length;
    int c = 0;
// repeating until all attributes are decoded
    do 
      var shift = 0;
      int result = 0;

      // for decoding value of one attribute
      do 
        c = list[index] - 63;
        result |= (c & 0x1F) << (shift * 5);
        index++;
        shift++;
       while (c >= 32);
      /* if value is negetive then bitwise not the value */
      if (result & 1 == 1) 
        result = ~result;
      
      var result1 = (result >> 1) * 0.00001;
      lList.add(result1);
     while (index < len);

/*adding to previous value as done in encoding */
    for (var i = 2; i < lList.length; i++) lList[i] += lList[i - 2];

    print(lList.toString());

    return lList;
  

  // ! SEND REQUEST
  void sendRequest(String intendedLocation) async 

   if(intendedLocation != null)

    List<Placemark> placemark =
        await Geolocator().placemarkFromAddress(intendedLocation);
    double latitude = placemark[0].position.latitude;
    double longitude = placemark[0].position.longitude;
    LatLng destination = LatLng(latitude, longitude);
    _addMarker(destination, intendedLocation);
    String route = await _googleMapsServices.getRouteCoordinates(
        _initialPosition, destination);
    createRoute(route);
    notifyListeners(); 
  

  // ! ON CAMERA MOVE
  void onCameraMove(CameraPosition position) 
    _lastPosition = position.target;
    notifyListeners();
  

  // ! ON CREATE
  void onCreated(GoogleMapController controller) 
    _mapController = controller;
              mapController.setMapStyle('[  "featureType": "all", "elementType": "labels.text.fill", "stylers": [  "color": "#7c93a3" ,  "lightness": "-10"  ] ,  "featureType": "administrative.country", "elementType": "geometry", "stylers": [  "visibility": "on"  ] ,  "featureType": "administrative.country", "elementType": "geometry.stroke", "stylers": [  "color": "#a0a4a5"  ] ,  "featureType": "administrative.province", "elementType": "geometry.stroke", "stylers": [  "color": "#62838e"  ] ,  "featureType": "landscape", "elementType": "geometry.fill", "stylers": [  "color": "#dde3e3"  ] ,  "featureType": "landscape.man_made", "elementType": "geometry.stroke", "stylers": [  "color": "#3f4a51" ,  "weight": "0.30"  ] ,  "featureType": "poi", "elementType": "all", "stylers": [  "visibility": "simplified"  ] ,  "featureType": "poi.attraction", "elementType": "all", "stylers": [  "visibility": "on"  ] ,  "featureType": "poi.business", "elementType": "all", "stylers": [  "visibility": "off"  ] ,  "featureType": "poi.government", "elementType": "all", "stylers": [  "visibility": "off"  ] ,  "featureType": "poi.park", "elementType": "all", "stylers": [  "visibility": "on"  ] ,  "featureType": "poi.place_of_worship", "elementType": "all", "stylers": [  "visibility": "off"  ] ,  "featureType": "poi.school", "elementType": "all", "stylers": [  "visibility": "off"  ] ,  "featureType": "poi.sports_complex", "elementType": "all", "stylers": [  "visibility": "off"  ] ,  "featureType": "road", "elementType": "all", "stylers": [  "saturation": "-100" ,  "visibility": "on"  ] ,  "featureType": "road", "elementType": "geometry.stroke", "stylers": [  "visibility": "on"  ] ,  "featureType": "road.highway", "elementType": "geometry.fill", "stylers": [  "color": "#bbcacf"  ] ,  "featureType": "road.highway", "elementType": "geometry.stroke", "stylers": [  "lightness": "0" ,  "color": "#bbcacf" ,  "weight": "0.50"  ] ,  "featureType": "road.highway", "elementType": "labels", "stylers": [  "visibility": "on"  ] ,  "featureType": "road.highway", "elementType": "labels.text", "stylers": [  "visibility": "on"  ] ,  "featureType": "road.highway.controlled_access", "elementType": "geometry.fill", "stylers": [  "color": "#ffffff"  ] ,  "featureType": "road.highway.controlled_access", "elementType": "geometry.stroke", "stylers": [  "color": "#a9b4b8"  ] ,  "featureType": "road.arterial", "elementType": "labels.icon", "stylers": [  "invert_lightness": true ,  "saturation": "-7" ,  "lightness": "3" ,  "gamma": "1.80" ,  "weight": "0.01"  ] ,  "featureType": "transit", "elementType": "all", "stylers": [  "visibility": "off"  ] ,  "featureType": "water", "elementType": "geometry.fill", "stylers": [  "color": "#a3c7df"  ]  ]');

    notifyListeners();
  


// FLOATING BUTTON TO LOCATION
 void toUserLocation()

_mapController.animateCamera(
      CameraUpdate.newCameraPosition(
        CameraPosition(
            target: initialPosition, zoom: 15.0),
      ),
    );

      

      void centerMap(String intendedLocation) async 


  List<Placemark> placemark =
        await Geolocator().placemarkFromAddress(intendedLocation);
    double latitude = placemark[0].position.latitude;
    double longitude = placemark[0].position.longitude;
    LatLng goToDestination = LatLng(latitude, longitude);   


_mapController.animateCamera(
      CameraUpdate.newLatLngBounds(
          LatLngBounds(
                southwest: LatLng(
                    _initialPosition.latitude <= goToDestination.latitude
                        ? _initialPosition.latitude
                        : goToDestination.latitude,
                    _initialPosition.longitude <= goToDestination.longitude
                        ? _initialPosition.longitude
                        : goToDestination.longitude),
                northeast: LatLng(
                    _initialPosition.latitude <= goToDestination.latitude
                        ? goToDestination.latitude
                        : _initialPosition.latitude,
                    _initialPosition.longitude <= goToDestination.longitude
                        ? goToDestination.longitude
                        : _initialPosition.longitude)),10.0),
    );

      

void clearMap()

_polyLines.clear();

_markers.clear();
 _mapController.animateCamera(
      CameraUpdate.newCameraPosition(
        CameraPosition(
            target: initialPosition, zoom: 15.0),
      ),
    );_mapController.animateCamera(
      CameraUpdate.newCameraPosition(
        CameraPosition(
            target: initialPosition, zoom: 15.0),
      ),
    );




//  LOADING INITIAL POSITION
  void _loadingInitialPosition()async
    await Future.delayed(Duration(seconds: 5)).then((v) 
      if(_initialPosition == null)
        locationServiceActive = false;
        notifyListeners();
      
    );
  

这是我的 pubspec.yaml 文件

name: toladriver
description: A new Flutter project.

version: 1.0.0+1

environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  geolocator: 5.3.2+2
  provider: 4.1.3
  http: 0.12.1
  flutter_google_places: 0.2.5
  geocoder: 0.2.1
  fluttertoast : 4.0.1
  flutter_spinkit: "^2.1.0"
  slide_countdown_clock: 1.0.3

  google_maps_flutter: 0.5.28+1 
  google_maps_webservice: 0.0.17  

  cupertino_icons: ^0.1.3

dev_dependencies:
  flutter_test:
    sdk: flutter

flutter:
  uses-material-design: true      
  fonts:
    - family: Clan-Medium
      fonts:
        - asset: fonts/clan-book-webfont.ttf
        - asset: fonts/clan-med-webfont.ttf
          

【问题讨论】:

Mac 你能解决这个问题吗? 【参考方案1】:

需要添加如下依赖:

dependencies:
  google_maps_flutter: ^0.5.28+1

https://pub.dev/packages/google_maps_flutter#-installing-tab-

【讨论】:

我做到了,我仍然得到一个空白屏幕并且抛出一个插件异常【参考方案2】:

确认您的 Google 地图 API 密钥是否有效。验证密钥时会引发该错误。

【讨论】:

以上是关于MissingPluginException(MissingPluginException(在通道 plugins.flutter.io/google_maps_53 上找不到方法 camera#an的主要内容,如果未能解决你的问题,请参考以下文章

MissingPluginException(未找到通道上的方法的实现)

GoogleMaps Flutter 插件 MissingPluginException

发布模式android中的MissingPluginException

尝试直接从 FLUTTER 发出呼叫:MissingPluginException(未找到方法 callNumber 的实现

android Unhandled Exception 上的 Flutter 运行错误:MissingPluginException(未找到方法的实现)

MissingPluginException(在通道 com.amazonaws.amplify/amplify 上找不到方法配置的实现)