2

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();
  }
}
Uzair Ahmad
  • 21
  • 1
  • 3
  • Can you post your entire error message or stack trace to better understand the error. – srihari ayapilla Jun 16 '22 at 07:39
  • Well, what do you *want* to do when these fields are `null`? We cannot magically summon correct geo-coordinates for you. You have to decide how to handle not having any. – nvoigt Jun 16 '22 at 08:48

2 Answers2

0

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.

Ahmed Adel
  • 305
  • 2
  • 9
  • Thanks For your help but it will fix this error and by providing 0 as Latitude and longitude it will cause issue where i am sending lat and long details to get some output using lat long. – Uzair Ahmad Jun 16 '22 at 08:15
  • It will only provide zero in case the latitude or longitude is null, you can try another solution by defining a default location (like user current location) then send its latitude and longitude in case of null – Ahmed Adel Jun 16 '22 at 10:20
0

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"];
Uzair Ahmad
  • 21
  • 1
  • 3