错误:
location.dart failed assertion: .... 'latitude != null': is not true.
显然,答案在这里:
Flutter - Google Maps doesn´t wait to Location
然而,这些东西都不适合我。
试图:
show an empty Container() while lat and lng is null...
我不知道这到底是什么... ==??像python一样?
纬度 == 空 || lng == 空
?容器()
我的猜测是这个人希望我将 lat 和 lng 分配为 null 并将谷歌地图放入容器中。这里什么都没有:
var lat = null;
var lng = null;
我将我的 sizebox 转换为容器并更改了初始相机位置:前:
SizedBox(
height: 350,
child: GoogleMap(
markers: Set.from(markers),
initialCameraPosition: _myLocation,
myLocationEnabled: true,
compassEnabled: true,
myLocationButtonEnabled: true,
mapType: MapType.normal,
onMapCreated: (GoogleMapController controller) {
controller.setMapStyle(Utils.mapStyles);
}),
后:Container(
height: 350,
child: GoogleMap(
markers: Set.from(markers),
initialCameraPosition: CameraPosition(
target: LatLng(lat, lng),
zoom: 15.0),
myLocationEnabled: true,
compassEnabled: true,
myLocationButtonEnabled: true,
mapType: MapType.normal,
onMapCreated: (GoogleMapController controller) {
controller.setMapStyle(Utils.mapStyles);
}),
),
结果:Failed assertion: 'latitude != null': is not true.
该程序现在甚至无法编译。戳了一下,我将目标更改为: target: LatLng(null, null),
同样的错误。什么也没有变。
<抹去一切,重新开始>
"you can display a loader until you get your location"
没用。
这就是我从谷歌表中调用我的经度和纬度点的方式。我正在尝试在按钮上绘制它们:
Future<void> _plotCurrent() async {
Map<double,double> m = (await sheet.values.map.column(3, fromRow:2)).map((key, value)=>
MapEntry(double.parse(key), double.parse(value)));
Map<double,double> m2 = (await sheet.values.map.column(4, fromRow:2)).map((key, value)=>
MapEntry(double.parse(key), double.parse(value)));
Iterable _markers = Iterable.generate(10, (index) {
LatLng latLngMarker = LatLng(m["test$index"], m2["test$index"]);
return Marker(markerId: MarkerId("test$index"),position: latLngMarker);
});
setState(() {
markers = _markers;
});
}
我已经阅读了一些关于必须将我的 Widget 构建树更改为 Future 类型的内容。但是,我对 Dart 仍然很糟糕。我不知道该怎么做。这能行吗?这是我的小部件的开始: @override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
resizeToAvoidBottomPadding: false,
appBar: AppBar(
title: Text(widget.title, style: TextStyle(fontSize: 11)),
centerTitle: true,
),
body:
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Form(
key: _formKey,
child: Padding(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
TextFormField(
请帮忙。谢谢你。编辑:
这是没有意义的。地理定位器是搞砸了还是什么?这是我现在拥有的:
@override
initState() {
super.initState();
getLocation();
}
var lng, lat;
Future getLocation() async {
Position position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
setState(() {
lat = position.latitude;
lng = position.longitude;
print(lng);
});
}
@override
Widget build(BuildContext context) {
if (lat == null || lng == null) {
return Container();
}
Container(
height: 350,
child: GoogleMap(
markers: Set.from(markers),
initialCameraPosition: CameraPosition(
target: LatLng(lat, lng),
zoom: 15.0),
myLocationEnabled: true,
compassEnabled: true,
myLocationButtonEnabled: true,
mapType: MapType.normal,
onMapCreated: (GoogleMapController controller) {
controller.setMapStyle(Utils.mapStyles);
}),
);
return Scaffold(
key: _scaffoldKey,
resizeToAvoidBottomPadding: false,
appBar: AppBar(
title: Text(widget.title, style: TextStyle(fontSize: 11)),
centerTitle: true,
),
body:
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Form(
key: _formKey,
child: Padding(
第一个问题是,我的 map 不再显示了。我可以打印坐标...所以,我不明白为什么错误仍然显示并且在我按下绘图后一切都崩溃了。有人吗?编辑2:
这可能与我的错误有关:
The relevant error-causing widget was: MyHomePage file:///C:/Users/xxx/Desktop/xxxxx/xxxx-xxx/lib/main.dart:92:13 When the exception was thrown, this was the stack:
指向这个小部件:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'xx',
theme: ThemeData(
primarySwatch: colorCustom,
hintColor: Colors.white,
canvasColor: colorCustom,
backgroundColor: Colors.red,
),
home: MyHomePage(title: 'xx'),
);
}
}
我对 Dart 没有足够的经验,无法知道将标题与我的其他小部件分开的主页有什么问题。这是坏事吗?我将我的小部件移动到其他地方并应用了容器。我的应用程序不会以 lat == null || 开头lng == null 语句并引发以下错误:
The following assertion was thrown building GoogleMap(state: _GoogleMapState#81111): No Directionality widget found.
我认为问题是我加载了太多垃圾。我迷路了。
void main() => runApp(
RestartWidget(child: MyApp()),
);
class RestartWidget extends StatefulWidget {
RestartWidget({this.child});
final Widget child;
static void restartApp(BuildContext context) {
context.findAncestorStateOfType<_RestartWidgetState>().restartApp();
}
@override
_RestartWidgetState createState() => _RestartWidgetState();
}
class _RestartWidgetState extends State<RestartWidget> {
Key key = UniqueKey();
void restartApp() {
setState(() {
key = UniqueKey();
});
}
@override
Widget build(BuildContext context) {
return KeyedSubtree(
key: key,
child: widget.child,
);
}
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'xxxxxxx',
theme: ThemeData(
primarySwatch: colorCustom,
hintColor: Colors.white,
canvasColor: colorCustom,
backgroundColor: Colors.red,
),
home: MyHomePage(title: 'xxxxxxx'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _formKey = GlobalKey<FormState>();
final _scaffoldKey = GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
等等...我迷失在所有这些小部件中。有些东西导致我的位置为空。我不知道。有人吗?
最佳答案
在处理异步数据加载时,您需要有一个占位符容器,直到您的数据被加载。在这种情况下,这是 lat 和 long 参数。
Widget methodReturningWidget() {
// In case your data is not ready, return empty container
if (lat == null || long == null) {
return Container();
}
// In case your data is present, return GoogleMap object
return Container(
height: 350,
child: GoogleMap(
markers: Set.from(markers),
initialCameraPosition: CameraPosition(
target: LatLng(lat, long),
zoom: 15.0),
myLocationEnabled: true,
compassEnabled: true,
myLocationButtonEnabled: true,
mapType: MapType.normal,
onMapCreated: (GoogleMapController controller) {
controller.setMapStyle(Utils.mapStyles);
}),
);
}
导致您的应用程序崩溃的部分代码在这里 LatLng(lat, long)
试图创建对象,但它的参数 lat 和 long 为空。
关于google-maps - 断言失败 : 'latitude != null' : is not true. 我已经尝试了几件事,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63081141/