'Can't handle exception from Geolocator properly flutter

I'm trying to build a Flutter application which needs to get the user position an then logs it to a server via an http request. I'm using the Geolocator package, which works fine but i'm not able to handle properly the exception it generates (I'm pretty new to Developing in general, let alone Flutter). In particular, no matter what I do, I can't display an Alert dialog containing the exception i get from Geolocator when the user denies access to the location... The TimeoutException works fine. I've tried everything I could like assigning a key to the scaffold and then passing it to the showDialog but with no success. the print in the Exception runs but the one in the showDialog doesn't and it always bring me back to the root screen. Do you have any idea of why it doesn't works?

Thank you in advance! P.S. This is my first SO question so feel free to let me know if I left out some information.

Future<Position> determinePosition() async {
 bool serviceEnabled;
 LocationPermission permission;

 serviceEnabled = await Geolocator.isLocationServiceEnabled();
 if (!serviceEnabled) {
   throw Exception('Location services are disabled.');
 }

 permission = await Geolocator.checkPermission();
 if (permission == LocationPermission.denied) {
   permission = await Geolocator.requestPermission();
   if (permission == LocationPermission.denied) {
     throw Exception('Location permissions are denied');
   }
 }

 if (permission == LocationPermission.deniedForever) {
   throw Exception(
       'Location permissions are permanently denied, we cannot request permissions.');
 }

 return await Geolocator.getCurrentPosition(
     timeLimit: Duration(seconds: 6),
     desiredAccuracy: LocationAccuracy.lowest);
}

               try {
                      Position position = await determinePosition();
                      timbr1.LATIT = position.latitude;
                      timbr1.LONGIT = position.longitude;
                      Log_timbr(context);
              } on TimeoutException {
                      showDialog(
                        context: context,
                        builder: (BuildContext context) {
                          return AlertDialog(
                            title: Text('Tempo scaduto GPS non acquisito!'),
                            actions: <Widget>[
                              TextButton(
                                child: const Text("OK"),
                                onPressed: () {
                                  Log_timbr(context);
                                },
                              ),
                            ],
                          );
                        },
                      );
             } on Exception {
                      print("Running");
                      showDialog(
                        context: timbrascaffoldKey.currentContext!,
                        builder: (BuildContext context) {
                          print("Notrunning");
                          return AlertDialog(
                            title:
                                Text('Prego attivare la posizione e riprovare'),
                            actions: <Widget>[
                              TextButton(
                                child: const Text("OK"),
                                onPressed: () {
                                  Navigator.pushNamed(context, '/Login');
                                },
                              ),
                            ],
                          );
                        },
                      );
                    }



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source