0

I'm new to flutter, I just want to ensure if the below code is correct, I want to check if the location permission was granted or no, if yes then get the current location and save into shared preferences and THEN go to the homepage route, otherwise go to the location page to ask the user for access his location

@override
void initState() {
  super.initState();
  checkLocation(context);
}



  void checkLocation(context) async {
    bool isGranted = await asyncFunction();
    if(isGranted)
    {
      updateSettingLocation();
      Navigator.of(context).pushNamed('/homepage');
    } else{
      Navigator.of(context).pushNamed('/location');
    }
  }
  void updateSettingLocation() async{
    final location = await currentLocation();
    settingsRepo.setCurrentLocation(location);
  }

  Future<Position> currentLocation() {
    return Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high)
        .then((location) {
      if (location != null) {
        print("Location: ${location.latitude},${location.longitude}");
      }
      return location;
    });
  }

  void updateCurrentLocation() async {
    Position position = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high);
    settingsRepo.setCurrentLocation(position);
  }


  Future<bool> asyncFunction() async {
    bool serviceEnabled;
    LocationPermission permission;
    permission = await Geolocator.checkPermission();
    serviceEnabled = await Geolocator.isLocationServiceEnabled();
    if (permission == LocationPermission.denied || !serviceEnabled || permission == LocationPermission.deniedForever) {
      print('location access is denied');
      return false;
    } else {
      print('location access is granted');
      return true;
    }
  }
Ismail Muhammad
  • 320
  • 3
  • 15

2 Answers2

0

As mentioned in this Stack Overflow answer , the following changes should sufficient

@override
void initState() {
  super.initState();
  WidgetsBinding.instance.addPostFrameCallback((_) => checkLocation(context));
}

Though I would like to point out that context is not available in initState (unless it's a variable that you've created and are managing)

sidrao2006
  • 1,228
  • 2
  • 10
  • 32
  • Ignore the context, just want to know if the code like that is correct in part of the flow steps and the priority of implementing the functions, it works with no issues but i need to know if this is the right way for writing the code specifically the async functions – Ismail Muhammad Oct 22 '21 at 18:40
  • Your implementation should work well as long as you don't need to use `setState`. If you do use it in `initState` without the 'post frame callback', you'll get an error saying the widget hasn't been mounted yet. – sidrao2006 Oct 23 '21 at 03:15
0

All the functions defined are correct and the methodology is also fine. It should work with no issues. However I would suggest instead of defining all the functions here in the widget class you should separate it out from the UI by creating a separate class (Example: LocationService) and then initialize that class here and then make use of the functions.