flutter 说我的代码在 place.placeLocation!.longitude;

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

这是一个 Flutterflow 插件,可按客户位置返回列表 我想知道那个总是报错的部分出了什么问题,我听说placePosition已经不存在了

List<PlacesRecord> getPlacesMaximumDistance(
  List<PlacesRecord> places,
  LatLng userGeo,
  double maxDistance,
) {
  /// MODIFY CODE ONLY BELOW THIS LINE

  // First create some emptyList
  List<PlacesRecord> placesList = [];
  List<double> listKm = [];
  double lat1 = userGeo.latitude;
  double lon1 = userGeo.longitude;
  // This iterates through the single documents "places" in the List
  for (PlacesRecord place in places) {
    double lat2 = place.placePosition!.latitude;
    double lon2 = place.placePosition!.longitude;
    // This is doing math for distance calculations on the surface of a spheroid
    var c = math.cos;
    var p = 0.017453292519943295;
    var a = 0.5 -
        c((lat2 - lat1) * p) / 2 +
        c(lat1 * p) * c(lat2 * p) * (1 - c((lon2 - lon1) * p)) / 2;
    // This is getting us the distance
    var d = (12742 * math.asin(math.sqrt(a)));
    String inString = d.toStringAsFixed(2); // '2.35'
    double inDouble = double.parse(inString);
    listKm.add(inDouble);
    // Sort the documents that will be returned by distance
    listKm.sort();
    int listKmIndex = listKm.indexWhere((dist) => dist == inDouble);
    // Check if the document we are currently processing is no farther away from userGeo than we defined as max.
    if (inDouble <= maxDistance) {
      // If its within our radius, add it to the list of places documents that will be returned
      placesList.insert(listKmIndex, place);
    }
  }
  return placesList;

  /// MODIFY CODE ONLY ABOVE THIS LINE
}

flutter 说我的代码在

中出现错误
double lat2 = place.placePosition!.latitude;
flutter dart geolocation flutterflow
1个回答
0
投票

place.placePosition
为 null,这会在尝试访问其纬度属性时导致空引用异常。

要解决此问题,您应该在访问 place.placePosition 的纬度和经度属性之前添加空检查。 希望这有帮助

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