不能将"Null"分配给类型 'Position' 的变量



我想使用"geolocator"获取用户的地理位置数据包中。其中的逻辑是,当我接收到地理位置时,我将其发送给用于Google Maps的控制器,一段时间后,地图将根据接收到的坐标进行传输。我写了一个函数,但是它抛出了一个错误:

类型为'Null'的值不能赋值给类型为'Null'的变量"位置"。尝试更改变量的类型,或强制转换

错误显示在那里

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);
});}

在此替换部分功能后

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);
});}

还有另一件事,当我在物理设备上运行应用程序时,调试器显示此消息: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.

这是什么,重要吗?非常感谢!

替换你的代码
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){}

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);
});}

相关内容

  • 没有找到相关文章

最新更新