How to pass selected value from function and give it to controller

Viewed 130

I have a function for picking map using the place picker dependency in flutter. In my code what I am looking for is to return a value from this place picker dependency and transfer it to controller in order to store it in firebase.

Here is my code for the picker function

 Future<LocationResult> showPlacePicker() async {
    LocationResult result = await Navigator.of(context).push(
      MaterialPageRoute(
        builder: (context) =>
            PlacePicker("MAPS API KEY HERE"),
      ),
    );
    return result;
  }

Value passing to controller code:

Container(
                  width: 270,
                  child: TextFormField(
                    cursorColor: Colors.white,
                    style: TextStyle(color: Theme.of(context).primaryColor),
                    decoration: InputDecoration(
                        focusedBorder: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(25.0),
                          borderSide: BorderSide(),
                        ),
                        enabledBorder: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(25.0),
                          borderSide: BorderSide(
                            width: 2.0,
                          ),
                        ),
                        hintText: "Select Location",
                        hintStyle: TextStyle(
                          color: Theme.of(context).primaryColor,
                          fontFamily: "San Francisco",
                        )),
                    onTap: () async {
                      showPlacePicker()
                          .then((result) => provider.controllerText);
                    },
                  ),
                ),

I have validation check on the controller of isEmpty so that's how I am getting to know that the controller is still empty so how can I pass the value to it. Note I can't put Navigator.pop in the function because its a dependency and it has its pre built screen for picking the location I can't use the Navigator.pop there it takes me back to home screen.

Edit: result. further parameters which are not required

I only want the selected location thats all I want I don't have any use for name or location or any of these parameters further. This is the package I'm using place_picker and the PlacePicker library opens this screen in which there's a button to select this location that location is what I want just that and pass it to my provide controller Picker Screen

1 Answers

The "arrow" notation:

(A) => B

Is a shorthand syntax for:

(A) {return B;}

They are both equivalent. They are both specifying functions. With the first way simple functions can be written with more readability. So if you only want to return a value or call another function you can write it more easily.

Since you don't want your function to return anything, and instead use it to store in a value, you can use the full notation:

onTap: () async {
                      showPlacePicker()
                          .then((result) {provider.controllerText = result;}
                                );
                },

If you look closely, you will see that the onTap is already using this. You can read more about the fat arrow notation here.

Related