steind.VY
steind.VY

Reputation: 338

LateError (LateInitializationError: Field '_state@590051772' has not been initialized.)

I don't understand why I'm catching an error in _mapController:

final MapController _mapController = MapController();

  @override
  void initState() {
    super.initState();
    _mapController.move(widget.latLng, _mapController.zoom);
  }

Upvotes: 0

Views: 542

Answers (3)

Kheiria Hamayel
Kheiria Hamayel

Reputation: 1

I think the problem arises because you start using the mapcontroller before the build function starts which has the necessary configuration for the map. Try to put this line

 _mapController.move(widget.latLng, _mapController.zoom); 

inside a method for example (_moveMap) and call it inside the build function when pressing a button.

like this:

 child: ElevatedButton(
        child:Icon(Icons.location_on_outlined,
        color: Colors.blue,) ,
        onPressed:_moveMap ,
      )),

Upvotes: 0

Ravindra S. Patil
Ravindra S. Patil

Reputation: 14865

Try below code

MapController? _mapController;
//or try this-> MapController _mapController;

  @override
  void initState() {
    super.initState();
    _mapController.move(widget.latLng, _mapController.zoom);
  }

Upvotes: 0

Daniel Roldán
Daniel Roldán

Reputation: 1556

I don't know but try like this ? :

  late MapController _mapController;

  @override
  void initState() {
    _mapController = MapController();
    _mapController.move(widget.latLng, _mapController.zoom);
    super.initState();
  }

Upvotes: 1

Related Questions