Q:

How to use Navigator.push() in Google maps widget (with context = null)?

I have a Flutter app where I'm using the Google Maps widget (just updated to 0.4.0) to display some markers on the screen. Each marker corresponds to a shop and has an info window with the name of the shop. I would like that when we tap on the info window, the page of the shop appears.
It does not seem so complicated, here is what I did :
Marker marker = Marker(
  consumeTapEvents: false,
  markerId: MarkerId(markerId.toString()),
  position:
      LatLng(truck.situation.position.lat, truck.situation.position.long),
  infoWindow: InfoWindow(
    title: truck.name,
    onTap: ({BuildContext context}) {
      Navigator.push(
          context,
          MaterialPageRoute(
              builder: (context) => TruckScreen(truck: truck)
          ),
      );
    },
  ),
);

But when I click on the window, nothing happens. 
I think I found what the problem is : when we click on the info window, the method _handleMethodCall(MethodCall call)  is called in the controller.dart file :
case 'infoWindow#onTap':
        _googleMapState.onInfoWindowTap(call.arguments['markerId']);
        break;

The context is not passed as a parameter and thus, when I go in my onTap method where I call Navigator.push, context is null.
Does someone know how to fix this ? Thanks !

A:

onTap method in Marker class does not pass context
google_maps_flutter/lib/src/marker.dart#L45
and because you are adding a context argument into onTap method callback it is overriding the context value from build method.
You need to get context from your build method. Just remove ({BuildContext context})
Marker marker = Marker(
  consumeTapEvents: false,
  markerId: MarkerId(markerId.toString()),
  position:
      LatLng(truck.situation.position.lat, truck.situation.position.long),
  infoWindow: InfoWindow(
    title: truck.name,
    onTap: () {
      Navigator.push(
        context,
        MaterialPageRoute(builder: (context) => TruckScreen(truck: truck)),
      );
    },
  ),
);

