SBR*_*R90 7 google-maps position dart flutter
我试图获取用户当前位置,但在 l.latitude 和 l.longitude 上出现此错误
参数类型“double?” 不能分配给参数类型“double”。
void _onMapCreated(GoogleMapController _cntlr) {
_controller = _cntlr;
_location.onLocationChanged.listen((l) {
_controller.animateCamera(
CameraUpdate.newCameraPosition(
CameraPosition(
target: LatLng(l.latitude, l.longitude),
zoom: 15,
),
),
);
});
}
Run Code Online (Sandbox Code Playgroud)
小智 21
您得到的错误来自空安全,该类型double?
意味着它可以是 adouble
或null
,但您的参数只接受 a double
,而不接受 no null
。
!
为此,您可以通过在变量末尾添加 a 来“强制”使用“非空”变量,但执行此操作时要小心。
CameraPosition(
target: LatLng(l.latitude!, l.longitude!),
zoom: 15,
)
Run Code Online (Sandbox Code Playgroud)
您可以在官方文档上了解更多有关 null-safety 语法和原理的信息: https: //flutter.dev/docs/null-safety
归档时间: |
|
查看次数: |
28206 次 |
最近记录: |