'Null' can't be assigned to a variable of type 'Position'

644 views Asked by At

I want to get geolocation data from the user using the "geolocator" package. The logic is such that when I receive a geolocation, I send it to the controller for Google Maps and after some time the map transfers according to the received coordinates. I wrote a function, but it throws an error :

A value of type 'Null' can't be assigned to a variable of type 'Position'. Try changing the type of the variable, or casting the right-hand type to 'Position'.

The error showing there

Future<void> _getCurrentPosition() async {
Position position = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.best, forceAndroidLocationManager: true)
    .then((Position position) async {
  final GoogleMapController controller = await _controller.future;
  controller.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
      target: LatLng(position.latitude, position.longitude), zoom: 14)));
}).catchError((e) {
  print(e);
});}

After replacing a part of function on this

Position? position = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.best, forceAndroidLocationManager: true)
    .then((Position position) async {
  final GoogleMapController controller = await _controller.future;
  controller.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
      target: LatLng(position.latitude, position.longitude), zoom: 14)));
}).catchError((e) {
  print(e);
});}

There comes another thing, when i run the app on physical device the debugger shows this messages:Restarted application in 1 796ms. 2022-05-01 23:39:05.165 Runner[2516/0x102610580] [lvl=3] +[GMSx_CCTClearcutUploader crashIfNecessary] Multiple instances of CCTClearcutUploader were instantiated. Multiple uploaders function correctly but have an adverse affect on battery performance due to lock contention. [tcp] tcp_input [C4.1:3] flags=[R] seq=611763406, ack=0, win=0 state=ESTABLISHED rcv_nxt=611763406, snd_una=1878468883 Connection 4: received failure notification Connection 4: received ECONNRESET with incomplete TLS handshake - generating errSSLClosedNoNotify Connection 4: failed to connect 3:-9816, reason -1 Connection 4: encountered error(3:-9816) Task <D01E9B91-BB3F-4674-AB74-5063E6362617>.<1> HTTP load failed, 0/0 bytes (error code: -1200 [3:-9816]) 2 [tcp] tcp_input [C4.1:3] flags=[R] seq=611763406, ack=0, win=0 state=CLOSED rcv_nxt=611763406, snd_una=1878468883 Task <D01E9B91-BB3F-4674-AB74-5063E6362617>.<1> finished with error [-1200] Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo={NSErrorFailingURLStringKey=https://clients4.google.com/glm/mmap, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, _NSURLErrorFailingURLSessionTaskErrorKey=LocalUploadTask <D01E9B91-BB3F-4674-AB74-5063E6362617>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalUploadTask <D01E9B91-BB3F-4674-AB74-5063E6362617>.<1>" ), NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made., NSErrorFailingURLKey=https://clients4.google.com/glm/mmap, NSUnderlyingError=0x28150ca50 {Error Domain=kCFErrorDomainCFNetwork Code=-1200 "(null)" UserInfo={_kCFStreamPropertySSLClientCertificateState=0, _kCFNetworkCFStreamSSLErrorOriginalValue=-9816, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9816, _NSURLErrorNWPathKey=satisfied (Path is satisfied), viable, interface: utun2, ipv4, dns, expensive}}, _kCFStreamErrorCodeKey=-9816} [tcp] tcp_input [C4.1:3] flags=[R] seq=611763406, ack=0, win=0 state=CLOSED rcv_nxt=611763406, snd_una=1878468883 -canOpenURL: failed for URL: "comgooglemaps://" - error: "This app is not allowed to query for scheme comgooglemaps" -canOpenURL: failed for URL: "googlechromes://" - error: "This app is not allowed to query for scheme googlechromes" Lost connection to device.

Whats is it and is this important? Thanks a lot!

1

There are 1 answers

2
Mäddin On BEST ANSWER

Replace your code with

try{
    Position position = await Geolocator.getCurrentPosition(
            desiredAccuracy: LocationAccuracy.best, forceAndroidLocationManager: true)
        ..then((Position position) async {
      final GoogleMapController controller = await _controller.future;
      controller.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
          target: LatLng(position.latitude, position.longitude), zoom: 14)));
    }).catchError((e) {
      print(e);
    });}
}catch(e){}

or

Position? position = await Geolocator.getCurrentPosition(
            desiredAccuracy: LocationAccuracy.best, forceAndroidLocationManager: true)
        .then((Position position) async {
      final GoogleMapController controller = await _controller.future;
      controller.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
          target: LatLng(position.latitude, position.longitude), zoom: 14)));
    }).catchError((e) {
      print(e);
    });}