1

I have this app : enter image description here

I want when click on floatingActionButton zoom out a little but it throw this exception

The method 'animateCamera' was called on null.
Receiver: null
Tried calling: animateCamera(Instance of 'CameraUpdate')

and this is my code :

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {


  @override
  void initState() {
  }

  @override
  Widget build(BuildContext context) {
    GoogleMapController _controller;
    final CameraPosition _initialPosition = CameraPosition(target: LatLng(24.903623, 67.198367));
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: GoogleMap(
        initialCameraPosition: _initialPosition,
        mapType: MapType.normal,
        onMapCreated: (controller){
          setState(() {
            _controller = controller;
          });
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: (){
          _controller.animateCamera(CameraUpdate.zoomOut());
        },
        child: Icon(Icons.zoom_out),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

}

how to fix it ?

Laila Mattar
  • 361
  • 4
  • 19
  • Hot restart app. Looks like you have added `_controller` after map is created – Tugay Nov 23 '20 at 12:41
  • 1
    Does this answer your question? [What is a NoSuchMethod error and how do I fix it?](https://stackoverflow.com/questions/64049102/what-is-a-nosuchmethod-error-and-how-do-i-fix-it) – nvoigt Nov 23 '20 at 13:49
  • Please refer https://medium.com/flutterdevs/location-in-flutter-27ca6fa1126c – sushant singh Jul 13 '21 at 11:19

2 Answers2

4

This is Because your mapController is null on which you call !

mapController.animateCamera();

sounds like you Decleare "GoogleMapController mapController;" like this

i will recommemd you to use Completer of type GoogleMapController and then intilize it within "OnMapCreated:" with the controller return by "OnMapCreated" function First Decleare Controller in Global section like this: Completer<GoogleMapController> _controller = Completer();

then Inside GoogleMaps use onMapCreated: function like below GoogleMaps(

onMapCreated: (GoogleMapController controller) {
              _controller.complete(controller);
            },
)

and now how to use this 1st) : before calling to animateCamera function use this

final GoogleMapController mapController = await _controller.future;

this will initilize mapController and then Call

 mapController.animateCamera()
Fakhar Ilyas
  • 129
  • 9
0

Restart your app. Also consider moving

GoogleMapController _controller;
    final CameraPosition _initialPosition = CameraPosition(target: LatLng(24.903623, 67.198367));

either into initstate or declare it as a member variable

FrontMobe
  • 186
  • 1
  • 13