Flutter:Geolocator返回在空值上调用了“ compareTo”方法

时间:2020-01-26 07:26:30

标签: android ios flutter dart geolocation

嗨,我正在尝试从我的主页获取用户位置,我不知道为什么,但是当用户在单击按钮后导航到“首页”时,出现错误:

在null上调用了方法'compareTo'。接收方:空尝试调用: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 个答案:

答案 0 :(得分:2)

我认为您应该使用标记来了解经纬度填充时间

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'
                      }),
                ])
        );
      }
    }