Flutter 错误:NoSuchMethodError (NoSuchMethodError: The method 'getDouble' was called on null

问题描述 投票:0回答:2

启动我的 Flutter 应用程序后,它显示以下错误:

NoSuchMethodError (NoSuchMethodError: The method 'getDouble' was called on null.
Receiver: null
Tried calling: getDouble("lat"))

这是应该导致错误的代码部分:

    _userCurrentLocationUpdate(Position updatedPosition) async {
    double distance = await Geolocator.distanceBetween(
        prefs.getDouble('lat'),
        prefs.getDouble('lng'),
        updatedPosition.latitude,
        updatedPosition.longitude);
    Map<String, dynamic> values = {
      "id": prefs.getString("id"),
      "position": updatedPosition.toJson()
    };
    if (distance >= 50) {
      if (show == Show.RIDER) {
        sendRequest(coordinates: requestModelFirebase.getCoordinates());
      }
      _userServices.updateUserData(values);
      await prefs.setDouble('lat', updatedPosition.latitude);
      await prefs.setDouble('lng', updatedPosition.longitude);
    }
  }
android flutter dart google-maps geolocation
2个回答
0
投票

getDouble
返回可为空的双精度值。您可以在 null 情况下提供默认值。

Geolocator.distanceBetween(
    prefs.getDouble('lat')?? .0,
    prefs.getDouble('lng')?? .0,

在保存(第一次运行)之前你不会有数据。


0
投票

您的错误和当前代码表明您在调用方法之前忘记初始化

prefs
变量。所以你需要初始化它。

© www.soinside.com 2019 - 2024. All rights reserved.