【发布时间】:2020-01-26 07:26:30
【问题描述】:
您好,我正在尝试从我的主页获取用户位置,但我不知道为什么,但是当用户在单击按钮后导航到主页时出现错误:
在 null 上调用了“compareTo”方法。接收方:null 尝试调用:compareTo(-90.0)
(我认为这是因为当时没有定义_latitude && _longitude,但是出了什么问题,为什么?)
home.dart:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:geolocator/geolocator.dart';
import 'package:latlong/latlong.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => new _HomePageState();
}
class _HomePageState extends State<HomePage> {
Geolocator geolocator = Geolocator();
double _latitude;
double _longitude;
@override
void initState() {
super.initState();
getLocation();
}
getLocation() async {
Position position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
try {
setState(() {
_latitude = position.latitude;
_longitude = position.longitude;
});
} on PlatformException catch (e) {
print(e);
}
print('Current location lat long ' + position.latitude.toString() + " - " + position.longitude.toString());
List<Placemark> placeMark = await Geolocator().placemarkFromCoordinates(position.latitude, position.longitude);
print('City name ' + placeMark[0].locality);
print('Country name ' + placeMark[0].country);
print('Postal Code ' + placeMark[0].postalCode);
}
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new FlutterMap(
options: new MapOptions(
center: new LatLng(_latitude,_longitude), minZoom: 5.0),
layers: [
new TileLayerOptions(
urlTemplate:
"https://api.mapbox.com/styles/v1/morraycage/ck5rzbzpa50mk1ioal9gjbavp/tiles/256/{z}/{x}/{y}@2x?access_token=XXXX",
additionalOptions: {
'accessToken': 'XXXX',
'id': 'mapbox.mapbox-streets-v7'
}),
])
);
}
}
感谢您的回答:D
【问题讨论】:
标签: android ios flutter dart geolocation