错误:flutter/lib/ui/ui_dart_state.cc(198)]未经手的例外:null检查运算符在空值上使用

发布于 2025-02-07 17:07:41 字数 1280 浏览 3 评论 0原文

大家好,我将面临问题(null检查操作员在null值上使用)时,当我删除此操作员时,它给我错误的是我的代码

Future<void> getDirectionDetails() async {
    var pickUpDetails =
        Provider.of<AppData>(context, listen: false).currentPickupAddress;
    var destinationDetails =
        Provider.of<AppData>(context, listen: false).destinationAddress;

    var pickUpLatLang =
        LatLng(pickUpDetails!.latitude!, pickUpDetails.longitude!);
    var destinationLatLang =
        LatLng(destinationDetails!.latitude!, destinationDetails.longitude!);

    showDialog(
        context: context,
        builder: (BuildContext context) =>
            ProgressDialog(status: "Please Wait"));

    var locationDetails = await GeocodeHelper.getDirectionDetails(
        pickUpLatLang, destinationLatLang);
    print(locationDetails.EncodingPoints);
    Navigator.pop(context);
  }

,这是我的班级。信息。

class AppData extends ChangeNotifier {
  Address? currentPickupAddress;
  Address? destinationAddress;

  void updatePickupAddress(Address pickup) {
    currentPickupAddress = pickup;
    notifyListeners();
  }

  void updateDestinationAddress(Address destination) {
    destinationAddress = destination;
    notifyListeners();
  }
}

Hi Everyone i am facing an issue (null check operator used on null value) when i remove this operator it gives me Error here is my code

Future<void> getDirectionDetails() async {
    var pickUpDetails =
        Provider.of<AppData>(context, listen: false).currentPickupAddress;
    var destinationDetails =
        Provider.of<AppData>(context, listen: false).destinationAddress;

    var pickUpLatLang =
        LatLng(pickUpDetails!.latitude!, pickUpDetails.longitude!);
    var destinationLatLang =
        LatLng(destinationDetails!.latitude!, destinationDetails.longitude!);

    showDialog(
        context: context,
        builder: (BuildContext context) =>
            ProgressDialog(status: "Please Wait"));

    var locationDetails = await GeocodeHelper.getDirectionDetails(
        pickUpLatLang, destinationLatLang);
    print(locationDetails.EncodingPoints);
    Navigator.pop(context);
  }

And this is my Class from where i am getting this information.

class AppData extends ChangeNotifier {
  Address? currentPickupAddress;
  Address? destinationAddress;

  void updatePickupAddress(Address pickup) {
    currentPickupAddress = pickup;
    notifyListeners();
  }

  void updateDestinationAddress(Address destination) {
    destinationAddress = destination;
    notifyListeners();
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

楠木可依 2025-02-14 17:07:42

我想问题是在这个代码块中

    var pickUpLatLang =
        LatLng(pickUpDetails!.latitude!, pickUpDetails.longitude!);
    var destinationLatLang =
        LatLng(destinationDetails!.latitude!, destinationDetails.longitude!);

看到您正在强迫拾音器,纬度和经度,而无需检查值是否为null,这给出了问题。

当然,您可以尝试以下内容,

    var pickUpLatLang =
        LatLng(pickUpDetails?.latitude??0, pickUpDetails?.longitude??0);
    var destinationLatLang =
        LatLng(destinationDetails?.latitude??0, destinationDetails?.longitude??0);

我假设纬度和经度是INT类型,如果不是,请提供合适的默认值。

I guess the problem is in this block of code

    var pickUpLatLang =
        LatLng(pickUpDetails!.latitude!, pickUpDetails.longitude!);
    var destinationLatLang =
        LatLng(destinationDetails!.latitude!, destinationDetails.longitude!);

See you are force unwrapping the pickUpDetails,latitude and longitude without checking if there value is null or not which gives the issue.

you can try the following

    var pickUpLatLang =
        LatLng(pickUpDetails?.latitude??0, pickUpDetails?.longitude??0);
    var destinationLatLang =
        LatLng(destinationDetails?.latitude??0, destinationDetails?.longitude??0);

Of course I'm assuming that latitude and longitude are of type int, if not please provide the suitable default value.

ι不睡觉的鱼゛ 2025-02-14 17:07:42

我只是重新检查我的代码,并从获得纬度和逻辑值的位置找到了一个错误。

currentData.latitude = response["result"]["geometry"]["location.lat"];
currentData.longitude = response["result"]["geometry"]["location.lng"];

之后

currentData.latitude = response["result"]["geometry"]["location"]["lat"];
currentData.longitude = response["result"]["geometry"]["location"]["lng"];

I've just recheck my code and found an error from where i was getting my latitude and logitude values.
Before

currentData.latitude = response["result"]["geometry"]["location.lat"];
currentData.longitude = response["result"]["geometry"]["location.lng"];

After

currentData.latitude = response["result"]["geometry"]["location"]["lat"];
currentData.longitude = response["result"]["geometry"]["location"]["lng"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文