Home > Net >  ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: Null check operator used on a null
ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: Null check operator used on a null

Time:06-16

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();
  }
}

CodePudding user response:

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.

CodePudding user response:

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"];
  • Related