title: How to use the geolocator package in flutter properly?
body: <p>I am using the geolocator package to get location in flutter app. But, in the app, sometimes the app gets location and the app works fine , but sometimes it does not works at all, and just the default initialised value is there. Here is the code of the services file:</p>
  
  <pre><code>import 'package:geolocator/geolocator.dart';
  import 'package:geocoder/geocoder.dart';
  
  class Location {
    double latitude;
    double longitude;
    String locationAddress = 'Getting Data';
  
    Future&lt;void&gt; getLocation() async {
      try {
        Position position = await Geolocator().getCurrentPosition(
          desiredAccuracy: LocationAccuracy.best,
        );
        latitude = position.latitude;
        longitude = position.longitude;
      } catch (e) {
        print(e);
        if (e.code == 'PERMISSION_DENIED') {
          print('denied');
        }
      }
    }
  
    Future&lt;void&gt; getUserLocation() async {
      //call this async method from whereever you need
      try {
        final coordinates = new Coordinates(latitude, longitude);
        var addresses =
            await Geocoder.local.findAddressesFromCoordinates(coordinates);
        var first = addresses.first;
        locationAddress = first.subLocality + ', ' + first.subAdminArea;
      } catch (e) {
        print(e);
      }
    }
  }
  </code></pre>
  
  <p>And this is the code of the button in which I want to get the locationAddress value:</p>
  
  <pre><code>class CurrentLocationButton extends StatefulWidget {
    @override
    _CurrentLocationButtonState createState() =&gt; _CurrentLocationButtonState();
  }
  
  class _CurrentLocationButtonState extends State&lt;CurrentLocationButton&gt; {
    String currentLocation = 'Getting Location';
  
    @override
    void initState() {
      super.initState();
      getLocationData();
    }
  
    void getLocationData() async {
      Location location = Location();
      await location.getLocation();
      await location.getUserLocation();
      setState(() {
        currentLocation = location.locationAddress;
      });
    }
  
    @override
    Widget build(BuildContext context) {
      return FlatButton(
        child: Text(
          'You are in\n$currentLocation',
          textAlign: TextAlign.center,
          style: TextStyle(
            fontSize: 22.0,
            fontWeight: FontWeight.w400,
            color: kWhiteColor,
          ),
        ),
        onPressed: () {},
      );
    }
  }
  </code></pre>
  
  <p>Sometimes, the app works and location is there in the button text, but sometimes just the text Getting Location is there (which is the initialised value).
  I just want that, as soon as the app opens, the location is there in the button text, and if the user denies for the permission, then the app should request for it again. How do I do this? </p>