lockApp method

Future<void> lockApp()

Locks the app immediately by showing the lock screen

This method will instantly lock the app, requiring authentication to access the content again. It first checks if authentication methods are available on the device before attempting to lock.

Example usage:

ElevatedButton(
  onPressed: () async {
    try {
      await context.lockApp();
    } catch (e) {
      // Handle case where authentication is not available
      print('Cannot lock app: $e');
    }
  },
  child: Text('Lock App'),
);

Throws:

  • StateError if called outside of a Guardo widget context
  • UnsupportedError if Guardo is disabled or no authentication methods are available

Implementation

Future<void> lockApp() async {
  final guardoState = _findGuardoStateNotifier();
  if (guardoState == null) {
    throw StateError(
      'No GuardoStateNotifier found in widget tree. '
      'Make sure this context is within a Guardo widget.',
    );
  }

  // Check if Guardo is enabled/functional
  if (!isGuardoEnabled()) {
    throw UnsupportedError(
      'Cannot lock app: Guardo authentication is disabled or not available. '
      'This could be due to disabled authentication or no security credentials available.',
    );
  }

  guardoState.showLockScreen();
}