【发布时间】:2021-11-03 08:21:09
【问题描述】:
我正在使用这个库在谷歌地图上显示我的当前位置:package:location/location.dart。 Pub Dev Link
问题是每当我以发布模式打开应用程序时,它会崩溃并显示:
LateInitializationError: 字段 'currentLatLng' 尚未 初始化
它不会在 Android 设备上的调试模式下崩溃。 但是,它确实会在 iOS (发布和调试模式)上崩溃。
我不确定我做错了什么。这是我的小部件和尝试:
class TestGoogle extends StatefulWidget {
const TestGoogle({Key? key}) : super(key: key);
@override
_TestGoogleState createState() => _TestGoogleState();
}
class _TestGoogleState extends State<TestGoogle> {
late LatLng currentLatLng;
late GoogleMapController mapController;
Location location = new Location();
var latitude;
var longitude;
late LocationData _locationData;
get() async {
_locationData = await location.getLocation();
latitude = _locationData.latitude;
longitude = _locationData.longitude;
setState(() {
currentLatLng = new LatLng(latitude, longitude);
});
}
@override
initState() {
super.initState();
get();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: GoogleMap(
myLocationEnabled: true,
onCameraMove: (CameraPosition cameraPosition) {
print(cameraPosition.zoom);
},
initialCameraPosition:
CameraPosition(target: currentLatLng, zoom: 15.0),
),
);
}
}
【问题讨论】:
-
我已经解决了你的问题、拼写语法等问题。你是否尝试过使用 flutter run -v --release 运行详细输出?请包括那个输出。