Home > Mobile >  Flutter Geolocator returning 0.0
Flutter Geolocator returning 0.0

Time:02-14

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();
  }}

CodePudding user response:

Use FutureBuilder instead of calling it inside initState...

  • Related