2

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

Ali Akbar
  • 121
  • 12
  • I'm not sure I understood what is the question. Do you want to store the ```showPlacePicker``` return value inside the ```provider.controllerText``` variable. Is that it? – Naslausky Mar 10 '22 at 12:08
  • Yes the showPlacePicker is returning result which is a selected location and I want it to store in provider.controllerText – Ali Akbar Mar 10 '22 at 12:14

1 Answers1

0

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.

Naslausky
  • 3,443
  • 1
  • 14
  • 24
  • I understand this but = result; is asking additional parameters like with the result. then it gives a lot of variables more. https://pub.dev/packages/place_picker this is the package I'm using I am not specifically taking all the parameters how can I get the location only – Ali Akbar Mar 10 '22 at 12:30
  • You should check the [reference](https://pub.dev/documentation/place_picker/latest/entities_location_result/LocationResult-class.html) of the package and choose the property that you need. Maybe ```result.name``` is what you are looking for? Otherwise you should edit your question and add the Error message you are getting, or what you are trying to obtain with some example. – Naslausky Mar 10 '22 at 13:00
  • I'm editing the question with the error image. Nothing works after result. – Ali Akbar Mar 10 '22 at 13:02