Ad

Flutter GetX Get.find() Returning Null Only At The First Time

- 1 answer

I am following uber_clone project on youtube. What the difference thing between tutorial and mine is that I am using GetX. I am facing the problem that I am getting null value for the first time when I use data from other controller.

dropOffLocation takes data from SeachController and I am tring to save dropOff data to firebase, but for the first time when I save the dropOff data it is null, so in firebase database stores null data of logitude and latitude of dropOff, but after I press cancel button that execute cancelRideRequest' method, and then, search destination, and then press request button that execute saveRideRequest`, finally I can store dropoff data in firebase database.

Why does it happen, how can I solve it?

// HomeController
  void saveRideRequest() {
    Rx<Address> dropOffLocation = SearchController.to.dropOffLocation;

    rideRequestRef =
        FirebaseDatabase.instance.reference().child("Ride Requests").push();

    var pickUp = pickUpLocation.value;
    var dropOff = dropOffLocation.value;
    print(dropOff.longitude);

    Map pickUpLocMap = {
      "latitude": pickUp.latitude.toString(),
      "logitude": pickUp.longitude.toString(),
    };

    Map dropOffLocMap = {
      "latitude": dropOff.latitude.toString(),
      "logitude": dropOff.longitude.toString(),
    };

    Map rideInfoMap = {
      "driver_id": "waiting",
      "payment_method": "cash",
      "pickup": pickUpLocMap,
      "dropoff": dropOffLocMap,
      "created_at": DateTime.now().toString(),
      "rider_name": userCurrentInfo!.name,
      "rider_phone": userCurrentInfo!.phone,
      "pickup_address": pickUp.placeName,
      "dropoff_address": dropOff.placeName,
    };

    rideRequestRef!.set(rideInfoMap);
  }


// ...
// ...

 void cancelRideRequest() {
    rideRequestRef!.remove();
  }

enter image description here

Ad

Answer

Yes you will get this error until a instance of your controller is created basically the reason is Get.find() finds a instance of already initiated controller in your project tree if your controller has not been initialized then it will simply return a null value. So before using Get.find() you have to create instance of your controller by using Get.put(YOUR_CONTROLLER); then you are free to use Get.find().

Ad
source: stackoverflow.com
Ad