skip to Main Content

first of all sorry for the messed up english, as it is not my first language.

So I’m trying to build a car rental app that uses maps api from google in flutter, I basically have a button to get the current location and after getting that location I want to pop the screen and get the saved data back to the parent screen as seen below

CHILD SCREEN

void savePlace(double lat, double lng) async {
    final url = Uri.parse(
        'https://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$lng&key=myapikey');

    final response = await http.get(url);

    final resData = json.decode(response.body);
    final address = resData['results'][0]["formatted_address"];

    pickedLocation = PickupLocation(
      latitude: lat,
      longitude: lng,
      address: address,
    );
    isGettingLocation = false;
    print("this first");
    print(pickedLocation);

    if (pickedLocation == null) {
      return;
    }
  }

after executing the code above I pop the pickedLocation data using the navigator pop function from flutter and thus the part where I print "this first" should run before I pop the screen because I put it before the pop screen, however in the parent screen the other code is run first

PARENT SCREEN

  void locationData() async {
    final selectedLocation = await Navigator.of(context).push(MaterialPageRoute(
      builder: (ctx) => ParentScreen(),
    ));

      setState(() {
        pickedLocation = selectedLocation;
      });
    

    print("should be second");
    print(pickedLocation);
  }

below is the output that is given by the debug console

I/flutter ( 7909): should be second
I/flutter ( 7909): null
I/flutter ( 7909): this first
I/flutter ( 7909): Instance of 'PickupLocation'

the pickedLocation became null because of it ran first instead of waiting for the selectedlocationdata is given

do tell me if you need extra information

2

Answers


  1. Chosen as BEST ANSWER

    I fixed the problem already, I simply run flutter clean and rebuild the app and the problem is fixed


  2. using this code may be your error can be resolved
    
    Navigator.Push(context, MaterialPageRoute(builder: (ctx) => ParentScreen())).then((value) {
                            setState((){
                              pickedLocation = selectedLocation;
                            });
                          });
    
    remove await keyword if not necessary
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search