my flutter app is crashing after I press the back arrow on the phone.
how to make the error happen:
1 - this button sends to a route that uses google maps.
ElevatedButton(
child: Text('Profesional'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => MyApp2()),
);
},
),
2-
This shows a live map that tracks the position of your phone.
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:flutter/material.dart';
import 'package:location/location.dart';
class MyApp2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
LatLng _initialcameraposition = LatLng(20.5937, 78.9629);
GoogleMapController _controller;
Location _location = Location();
void _onMapCreated(GoogleMapController _cntlr) {
_controller = _cntlr;
_location.onLocationChanged.listen((l) {
_controller.animateCamera(
CameraUpdate.newCameraPosition(
CameraPosition(target: LatLng(l.latitude, l.longitude), zoom: 15),
),
);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: Stack(
children: [
GoogleMap(
initialCameraPosition:
CameraPosition(target: _initialcameraposition),
mapType: MapType.normal,
onMapCreated: _onMapCreated,
myLocationEnabled: true,
),
],
),
),
);
}
}
3 -
When I press the back button of the phone, the app crashes and shows me this. which is inside a platform_channel.dart file that I believe is created automatically.
BinaryMessenger get binaryMessenger => _binaryMessenger ?? ServicesBinding.instance!.defaultBinaryMessenger;
final BinaryMessenger? _binaryMessenger;
@optionalTypeArgs
Future<T?> _invokeMethod<T>(String method, { required bool missingOk, dynamic arguments }) async {
assert(method != null);
final ByteData? result = await binaryMessenger.send(
name,
codec.encodeMethodCall(MethodCall(method, arguments)),
);
if (result == null) {
if (missingOk) {
return null;
}
*throw MissingPluginException('No implementation found for method $method on channel $name');*
}
return codec.decodeEnvelope(result) as T?;
}
the program (Visual studio ) highlights the line that I put between * *.