首页 > 解决方案 > 在运行地图时遇到问题,无一例外 googleMaps&Flutter

问题描述

每次我尝试打开地图时都会收到此错误

我不知道为什么会这样

我在网上搜索解决方案虽然还没有找到

它还说-在构建 CreateMapMarker(dirty, state: _CreateMapMarkerState#6a02b) 时抛出了以下断言:

感谢您的帮助感谢您的帮助感谢您的帮助

'package:google_maps_flutter_platform_interface/src/types/camera.dart': Failed assertion: line 26 pos 16: 'target != null': is not true.

import 'dart:async';
import 'package:geolocator/geolocator.dart';

import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class CreateMapMarker extends StatefulWidget {
  @override
  _CreateMapMarkerState createState() => _CreateMapMarkerState();
}

class _CreateMapMarkerState extends State<CreateMapMarker> {
      @override
        void initState(){
          super.initState();
          doSomeAsyncStuff();

        }
   Future<void> doSomeAsyncStuff() async {
   Geolocator().getCurrentPosition().then((currloc){
            setState((){
              _center = LatLng(currloc.latitude,currloc.longitude);
            });
          });
}


  Completer<GoogleMapController> _controller = Completer();
   static  LatLng _center;
    bool _isChecked = true;
  final Set<Marker> _markers = {};
  void _onMapCreated(GoogleMapController controller) {

        Geolocator().getCurrentPosition().then((currloc){
          _center = LatLng(currloc.latitude,currloc.longitude);
        });
    _controller.complete(controller);

  }



  @override
  Widget build(BuildContext context) {

        return MaterialApp(

          home: Scaffold(
            appBar: AppBar(
              title: Center(child:Text('Create Marker')),
              backgroundColor: Colors.green[700],
            ),
            body: GoogleMap(
              markers: _markers,
              onTap: _handleTap,
                        onMapCreated: _onMapCreated,
                        initialCameraPosition: CameraPosition(
                          target: _center,
                          zoom: 11.0,

                        ),
                      ),
                    ),
                  );
                }

                _handleTap(LatLng point) {
                setState(() {

                  _markers.add(Marker(

                    markerId: MarkerId(point.toString()),
                    position: point,
                    infoWindow: InfoWindow(
                      title: 'I am a marker',
                    ),

                    icon:
                        BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueMagenta),
                    onTap:()=> showModalBottomSheet(
                      context: this.context, 
                      builder: (builder){
                        return Container(
                          child:Center(
                            child: new Column(
                          children: <Widget>[
                            TextFormField(
                              decoration: InputDecoration(
                                labelText: 'Enter name',

                              ),
                            ),
                            TextFormField(
                              decoration: InputDecoration(
                                labelText: 'Enter type',

                              ),
                            ),
                            CheckboxListTile(
                              title: Text("Active ?"),
                              value: _isChecked,
                              onChanged: (newValue) { 
                                          setState(() {
                                            _isChecked = newValue; 
                                          }); 
                                        },
                              controlAffinity: ListTileControlAffinity.leading,  //  <-- leading Checkbox
                            ),
                            ],
                            ),
                        ));  

                      }
                      )
                  )
                  );
                });
}


}

标签: google-mapsflutter

解决方案


您收到错误是因为最初 _center 将为 null,因为我们正在处理 Future(即异步操作)。此外,“initialCameraPosition”用于设置地图相机的初始位置。如果您想在创建地图后更新相机位置,则可以使用 GoogleMapController 实例的“moveCamera”方法。这主要用于更改地图相机位置。看看下面更新的代码:

    class MapPage extends StatefulWidget {
  MapPage({Key key}) : super(key: key);

  @override
  _MapPageState createState() => _MapPageState();
}

class _MapPageState extends State<MapPage> {
  GoogleMapController _controller;
  bool _isChecked = true;
  final Set<Marker> _markers = {};

  @override
  void initState() {
    super.initState();
    doSomeAsyncStuff();
  }

  void _onMapCreated(GoogleMapController controller) {
    _controller = controller;
  }

  Future<void> doSomeAsyncStuff() async {
    Geolocator().getCurrentPosition().then((currloc) {
      print("current location $currloc");
      if (currloc != null) {
        _controller.moveCamera(
          CameraUpdate.newCameraPosition(
            CameraPosition(
              bearing: 270.0,
              target: LatLng(currloc.latitude, currloc.longitude),
              tilt: 30.0,
              zoom: 17.0,
            ),
          ),
        );
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Center(child: Text('Create Marker')),
        backgroundColor: Colors.green[700],
      ),
      body: GoogleMap(
        markers: _markers,
        onTap: _handleTap,
        onMapCreated: _onMapCreated,
        initialCameraPosition: CameraPosition(
          target: LatLng(0.0, 0.0),
          zoom: 11.0,
        ),
      ),
    );
  }

  _handleTap(LatLng point) {
    setState(() {
      _markers.add(Marker(
          markerId: MarkerId(point.toString()),
          position: point,
          infoWindow: InfoWindow(
            title: 'I am a marker',
          ),
          icon: BitmapDescriptor.defaultMarkerWithHue(
              BitmapDescriptor.hueMagenta),
          onTap: () => showModalBottomSheet(
              context: this.context,
              builder: (builder) {
                return Container(
                    child: Center(
                  child: new Column(
                    children: <Widget>[
                      TextFormField(
                        decoration: InputDecoration(
                          labelText: 'Enter name',
                        ),
                      ),
                      TextFormField(
                        decoration: InputDecoration(
                          labelText: 'Enter type',
                        ),
                      ),
                      CheckboxListTile(
                        title: Text("Active ?"),
                        value: _isChecked,
                        onChanged: (newValue) {
                          setState(() {
                            _isChecked = newValue;
                          });
                        },
                        controlAffinity: ListTileControlAffinity
                            .leading, //  <-- leading Checkbox
                      ),
                    ],
                  ),
                ));
              })));
       });
      }
      }

推荐阅读