Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
607 views
in Technique[技术] by (71.8m points)

How to solve "Failed assertion: line 22 pos 16: 'target != null' : is not true" in flutter

I am implementing google map in my flutter project and I am getting this error Failed assertion: line 22 pos 16: 'target != null': is not true as a red error screen for 2 seconds on my mobile device, and then the map got implemented.

How to solve that ?

here is the code:

body: GoogleMap(
        polylines: _polyLines,
        markers: _markers,
        mapType: MapType.normal,
        initialCameraPosition: CameraPosition(
          target: latLng,
          zoom: 14,
        ),
        onCameraMove: onCameraMove,
        onMapCreated: (GoogleMapController controller) {
          _controller.complete(controller);
        },
        myLocationEnabled: true,
      ),
question from:https://stackoverflow.com/questions/65932017/how-to-solve-failed-assertion-line-22-pos-16-target-null-is-not-true

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Answer First

SO what I here do, first I get current location using the geolocator: ^6.1.13 plugin and simply save it into one variable and until get current location show empty container and then pass the current location to the target value in Google map

class SelectionScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _selectionScreen();
  }
}

class _selectionScreen extends State<SelectionScreen> {
  @override
  initState() {
    loading = false;
    getCurrentLocation();
    super.initState();
  }
 bool loading;
  var start_currentPostion;

  getCurrentLocation() async {
    Position position = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high);

    setState(() {
      double latitude = position.latitude;
      double longitude = position.longitude;
      start_currentPostion = LatLng(latitude, longitude);
      loading = true;
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
        body: loading ? GoogleMap(
          mapType: MapType.normal,
          initialCameraPosition: CameraPosition(
            target: start_currentPostion,
            zoom: 14,
          ),
          onMapCreated: (GoogleMapController controller) {},
          myLocationEnabled: true,
        ) : Container());
  }
}

Answer Second

So in your GoogleMapPage You just need to add one boolean value and update this on the basis of location

bool getLocationLoading;

Add this value in your initState() and set it as false after that again call this boolean variable to your getLocation() method and set it as true

  @override
  void initState() {
    super.initState();
    getLocation();
    getLocationLoading = false;   // set false
  }

  getLocation() async {
    var location = new Location();
    location.onLocationChanged.listen((currentLocation) {
      print(currentLocation.latitude);
      print(currentLocation.longitude);
      setState(() {
        latLng =  LatLng(currentLocation.latitude, currentLocation.longitude);
      });

      print("getLocation:$latLng");
      _onAddMarkerButtonPressed();
      getLocationLoading = true;   // set true

    });

  }

and then simply add your GoogleMap to your body part

body: getLocationLoading ? GoogleMap(
  polylines: _polyLines,
  markers: _markers,
  mapType: MapType.normal,
  initialCameraPosition: CameraPosition(
  target: latLng,
  zoom: 14,
  ),
  onCameraMove: onCameraMove,
  onMapCreated: (GoogleMapController controller) {
  _controller.complete(controller);
  },
  myLocationEnabled: true,
  ), : Container());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...