Flutter:Geolocator返回方法'compareTo'在null上被调用
Posted
技术标签:
【中文标题】Flutter:Geolocator返回方法\'compareTo\'在null上被调用【英文标题】:Flutter: Geolocator return the method 'compareTo' was called on nullFlutter:Geolocator返回方法'compareTo'在null上被调用 【发布时间】: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
【问题讨论】:
【参考方案1】:我认为您应该使用标志来知道何时填充纬度和经度
class HomePage extends StatefulWidget
@override
_HomePageState createState() => new _HomePageState();
class _HomePageState extends State<HomePage>
Geolocator geolocator = Geolocator();
double _latitude;
double _longitude;
bool _isGettingLocation;
@override
void initState()
super.initState();
_isGettingLocation = true;
getLocation();
getLocation() async
Position position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
try
setState(()
_latitude = position.latitude;
_longitude = position.longitude;
_isGettingLocation = false;
);
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 _isGettingLocation ? Center(
child : CircularProgressIndicator()
) : 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'
),
])
);
【讨论】:
我很想,但我有这条消息Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score.
为我的问题投票可能会获得积分,我可以接受你的回答以上是关于Flutter:Geolocator返回方法'compareTo'在null上被调用的主要内容,如果未能解决你的问题,请参考以下文章
未找到 Flutter 插件“geolocator-Swift.h”文件
使用 geolocator 和 provider 以及 flutter_maps 获取位置
Flutter Geolocator 无法在 Android 上运行,但可以在 iOS 上完美运行