Harsha Vardhan
Harsha Vardhan

Reputation: 1088

How to update my location after location turned on in flutter

I am new to flutter and I am trying something to achieve in this example, I want to update user location after user turns location on, say for suppose user didn't turn on his location first after we give user a pop up saying this application need location on then it should update data but in the below example its not working, please help me out.

Here is the example what I am working on

PS:

enter image description here

Upvotes: 4

Views: 3543

Answers (1)

Niklas Raab
Niklas Raab

Reputation: 1736

Just subscribe to "onLocationChanged" Stream like in the example.

_location.onLocationChanged().listen((Map<String,double> result) {
          var latitude = result["latitude"]; //This is called always when the location updates
          var longitude = result["longitude"];
        });

For showing a popup when the user has no location enabled use this:

try {
  currentLocation = await location.getLocation;
} on PlatformException {
await showDialog<dynamic>(
              context: context,
              builder: (context) {
                return AlertDialog(
                  title: Text("No Location"),
                  content: Text(
                      "Please allow this App to use Location or turn on your GPS."),
                  actions: <Widget>[
                    FlatButton(
                      child: Text(
                        "Ok"
                      ),
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                    )
                  ],
                );
              });
}

Upvotes: 1

Related Questions