0

I am calling LocalAuth.Authenticate() when a button is pressed. I tried to run the app with no Passcode/PIN set and therefore it should raise a "NotAvailable" error. But this error is not caught by the try-catch block and instead just outputs the error in debug console. Any help is appreciated!

Here's my home_page.dart code:

                     TextButton(
                        child: Text('YES'),
                        onPressed: () async {
                          try {
                            final authenticated = await LocalAuth.authenticate();
                            print(authenticated);
                            if (authenticated) {
                              DatabaseHelper.instance.deleteModel(id as int);
                              Navigator.pop(context);
                            }
                          } on PlatformException catch (e) {
                            print(e);
                          }
                        },
                      )

Debug Console output:

I/flutter (10972): error PlatformException(NotAvailable, Required security features not enabled, null, null)

  • Is the `print(e);` statement executed when this error is thrown? – Peter Koltai Jan 04 '23 at 17:50
  • No its not. The code is not entering the catch block at all. I even tried removing on PlatformException and just catching all errors, but it still didnt execute the catch block. – Laxman Prasad Jan 04 '23 at 17:55
  • What is the code in `LocalAuth.authenticate()`? It should contain `throw` or `rethrow` in order to catch it. – Peter Koltai Jan 04 '23 at 17:58
  • That was it! Apparently the try catch block inside the authenticate method was firing first. Thanks alot! – Laxman Prasad Jan 04 '23 at 18:14
  • You are welcome, if you call your own functions from a try / catch block you always have to throw your own errors or rethrow the errors coming from other functions. – Peter Koltai Jan 04 '23 at 18:18

0 Answers0