flutter 谷歌Map相机错误:未处理的异常:MissingPluginException(找不到方法camera#animate的实现

tzdcorbm  于 2023-02-13  发布在  Flutter
关注(0)|答案(1)|浏览(147)

我是实现谷歌Map在flutter我的一切工作正常,直到我得到这个错误,
应用程序的方向流程为:login => Mappage =>dashboard => Mappage
登录后,Map页面工作正常,但当转到 Jmeter 板并再次返回到Map页面时,显示此错误:

unhandled Exception: MissingPluginException(No implementation found for method camera#animate on channel plugins.flutter.dev/google_maps_android_0)

我在代码中多次使用animateCamera方法,但第一次(登录后的Mappage)就成功了,这是因为completer吗?请解释
下面是我代码:
标测点:

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

import '../../StateManagement/MapController.dart';
import '../../drawers/bottomnavbar.dart';

class Mappage extends StatelessWidget {
   const Mappage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    MapController mapcontroller = Get.find();
    return Scaffold(
        body: Stack(
          children: [
            SizedBox(height: MediaQuery.of(context).size.height,
              child: SingleChildScrollView(
                child: SizedBox(height: MediaQuery.of(context).size.height,width: MediaQuery.of(context).size.width,
                  child: Column(
                    children: [
                      Stack(
                        children: [
                          SizedBox(
                            height: MediaQuery.of(context).size.height,
                            child: GetBuilder<MapController>(builder: (_)=>GoogleMap(initialCameraPosition:MapController.initial,
                              mapType: MapType.normal,markers:mapcontroller.markers,
                                onMapCreated: (GoogleMapController controller){
                                mapcontroller.completercontrol.complete(controller);
                                mapcontroller.googleMapController = controller;
                              },),)
                          ),
                          Positioned(
                            bottom: 50,
                            width: MediaQuery.of(context).size.width,
                            child: Row(mainAxisAlignment: MainAxisAlignment.spaceAround,
                              children: [
                                ElevatedButton(
                                    style:const ButtonStyle(backgroundColor:MaterialStatePropertyAll(Colors.redAccent)) ,
                                    onPressed: () {
                                      Get.offAll(()=>Nav());
                                    },
                                    child:const Text('Go to Dashboard',style: TextStyle(color: Colors.white),)),
                                FloatingActionButton(
                                    onPressed: () {
                                      mapcontroller.getlatlong();
                                    },
                                    child:const Icon(Icons.location_on,color: Colors.white,)),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
              ),
            ),
            GetBuilder<MapController>(builder: (_){
              if (mapcontroller.isloading == true) {
                return Container(
                  color: Colors.white.withOpacity(0.5),
                  child: const Center(
                    child: CircularProgressIndicator(backgroundColor: Colors.redAccent,color: Colors.white,),
                  ),
                );
              } else {
                return const SizedBox.shrink();
              }
            }),
          ],
        ));
  }
}

Map控制器(使用getx):

import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:geolocator/geolocator.dart';
import 'package:get/get.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

class MapController extends GetxController{
  String locationmessage = 'currentlocation of user';
  late String lat ;
  late String long ;

  Set<Marker>markers={};

  @override
  void onInit() {
    lat = '10.228370';
    long ='76.198799';
    super.onInit();
  }

  late GoogleMapController googleMapController;

  final Completer<GoogleMapController> _controller = Completer();
  Completer<GoogleMapController> get completercontrol => _controller;

  static  CameraPosition initial =  const CameraPosition(target:LatLng(10.228370,76.198799),zoom: 15);

  //lower part

  Future<Position> getCurrentLocation() async {
    bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
    if (!serviceEnabled) {
      return Future.error('the location is not enabled');
    }
    LocationPermission permission = await Geolocator.checkPermission();
    if (permission == LocationPermission.denied) {
      permission = await Geolocator.requestPermission();
      if (permission == LocationPermission.denied) {
        return Future.error(
            'location permissions are permanantly denied, cannot grant acess');
      }
    }
    if (permission == LocationPermission.deniedForever) {
      return Future.error(
          'location permissions are permanantly denied, cannot grant acess');
    }
    return await Geolocator.getCurrentPosition();
  }

  void liveLocation() {
    loadingbar();
    LocationSettings settings = const LocationSettings(
      accuracy: LocationAccuracy.high,
      distanceFilter: 100,
    );
    Geolocator.getPositionStream(locationSettings: settings)
        .listen((Position position) {
      lat = position.latitude.toString();
      long = position.longitude.toString();
      googleMapController.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(target:LatLng(position.latitude,position.longitude),zoom: 15)));
      markers.clear();
      markers.add(Marker(markerId:const MarkerId('current user location'),position: LatLng(position.latitude,position.longitude)));
      update();
    });
    loadingbaroff();
  }

  void getlatlong(){
    loadingbar();
    getCurrentLocation().then((value){
      lat = '${value.latitude}';
      long = '${value.longitude}';
      googleMapController.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(target:LatLng(value.latitude,value.longitude),zoom: 15)));
      markers.clear();
      markers.add(Marker(markerId:const MarkerId('current user location'),position: LatLng(value.latitude,value.longitude)));
      update();
      if (kDebugMode) {
        print(lat);
      }
      if (kDebugMode) {
        print(long);
      }
      liveLocation();
    });
    loadingbaroff();
  }

  void liveLocationToUpload(){
    LocationSettings settings = const LocationSettings(
      accuracy: LocationAccuracy.high,
      distanceFilter: 100,
    );
    Geolocator.getPositionStream(locationSettings: settings)
        .listen((Position position) {
      lat = position.latitude.toString();
      long = position.longitude.toString();
      update();
    });
  }

  //MAP PAGE CONTROLLER CONTROLLS :::::::::::::;

  Future<void> fetchMap()async {
    //markers.clear();
    //markers.add(Marker(markerId:const MarkerId('current user location'),position: LatLng(position.latitude,position.longitude)));
  }



  bool isloading = false;

  void loadingbar() {
    isloading = true;
    update();
  }
  void loadingbaroff() {
    isloading = false;
    update();
  }

}
plicqrtu

plicqrtu1#

首先,让我们试着隔离这个问题。你有没有试过导航到另一个页面不同的“ Jmeter 板”后,在“Map页”?尝试它,并告诉我,如果发生同样的问题,因为你说的问题发生时,你离开“Map页”,回到“Map页”。

相关问题