Flutter Geolocator returning 0.0

Viewed 74

I'm looking for little help with this code, I'm simply trying to get he High Accuracy Lon and Lat, but it keeps coming out with 0.0

What am I missing?

class _AdminState extends State<Admin> {
  bool showSpinner = false;
  double longitude = 0;
  double latitude = 0;

  @override
  void initState() {
    super.initState();
    getLocation();
  }

  late Position position;

  void getLocation() async {{
    bool serviceEnabled;
    LocationPermission permission;
    serviceEnabled = await Geolocator.isLocationServiceEnabled();
    if (!serviceEnabled) {
      return Future.error('Location services are disabled.');
    }
    permission = await Geolocator.checkPermission();
    if (permission == LocationPermission.denied) {
      permission = await Geolocator.requestPermission();
      if (permission == LocationPermission.denied) {
        return Future.error('Location permissions are denied');
      }
    }
    if (permission == LocationPermission.deniedForever) {
      return Future.error(
          'Location permissions are permanently denied, we cannot request permissions.');
    }
    //Permission ok, get Co-ords
    position =
    await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    longitude = position.longitude.toDouble();
    latitude = position.latitude.toDouble();
  }}
1 Answers

Use FutureBuilder instead of calling it inside initState...

Related