【问题标题】: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

【问题讨论】:

    标签: android ios flutter dart geolocation


    【解决方案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.
    • 为我的问题投票可能会获得积分,我可以接受你的回答
    猜你喜欢
    • 2021-02-26
    • 2023-04-05
    • 2021-08-22
    • 2021-11-30
    • 2020-11-10
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    相关资源
    最近更新 更多