Biometric authentication lets users unlock apps using their fingerprint or face. It makes apps safer and easier to use without typing passwords.
Biometric authentication in Flutter
final LocalAuthentication auth = LocalAuthentication(); bool canCheckBiometrics = await auth.canCheckBiometrics; bool authenticated = await auth.authenticate( localizedReason: 'Please authenticate to access', options: const AuthenticationOptions( biometricOnly: true, ), );
Use the local_auth package to access biometric features.
authenticate shows the biometric prompt to the user.
final LocalAuthentication auth = LocalAuthentication();
bool canCheck = await auth.canCheckBiometrics;bool authenticated = await auth.authenticate(
localizedReason: 'Scan your fingerprint to continue',
);bool authenticated = await auth.authenticate( localizedReason: 'Use face or fingerprint', options: const AuthenticationOptions( biometricOnly: true, stickyAuth: true, ), );
This app shows a button to start biometric authentication. It checks if biometrics are available, then asks the user to authenticate. The message updates to show success or failure.
import 'package:flutter/material.dart'; import 'package:local_auth/local_auth.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return const MaterialApp( home: BiometricAuthPage(), ); } } class BiometricAuthPage extends StatefulWidget { const BiometricAuthPage({super.key}); @override State<BiometricAuthPage> createState() => _BiometricAuthPageState(); } class _BiometricAuthPageState extends State<BiometricAuthPage> { final LocalAuthentication auth = LocalAuthentication(); String _message = 'Press the button to authenticate'; Future<void> _authenticate() async { bool canCheck = await auth.canCheckBiometrics; if (!canCheck) { setState(() { _message = 'Biometrics not available'; }); return; } try { bool authenticated = await auth.authenticate( localizedReason: 'Please authenticate to access', options: const AuthenticationOptions(biometricOnly: true), ); setState(() { _message = authenticated ? 'Authentication successful' : 'Authentication failed'; }); } catch (e) { setState(() { _message = 'Error: $e'; }); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Biometric Authentication')), body: Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ Text(_message, textAlign: TextAlign.center), const SizedBox(height: 20), ElevatedButton( onPressed: _authenticate, child: const Text('Authenticate'), ), ], ), ), ); } }
Test biometric features on a real device or emulator with biometrics enabled.
Always handle exceptions to avoid app crashes.
Biometric prompts are controlled by the device OS for security.
Biometric authentication uses fingerprint or face to secure apps.
Use the local_auth package in Flutter to access biometrics.
Check availability before asking the user to authenticate.
Practice
Solution
Step 1: Understand biometric authentication
Biometric authentication uses unique physical traits like fingerprint or face to secure access.Step 2: Identify its purpose in apps
It protects sensitive data by allowing only authorized users to enter.Final Answer:
To secure the app using fingerprint or face recognition -> Option DQuick Check:
Biometric authentication = Secure app access [OK]
- Confusing biometric authentication with UI changes
- Thinking it improves app speed
- Assuming it sends notifications
Solution
Step 1: Recall Flutter packages for biometrics
Thelocal_authpackage provides access to fingerprint and face authentication.Step 2: Compare with other packages
httpis for network calls,providerfor state management, andshared_preferencesfor storing data.Final Answer:
local_auth -> Option AQuick Check:
Biometric package = local_auth [OK]
- Choosing http for biometrics
- Confusing provider with authentication
- Picking shared_preferences for biometrics
final isAvailable = await auth.canCheckBiometrics;
if (isAvailable) {
final didAuthenticate = await auth.authenticate(
localizedReason: 'Please authenticate',
);
print(didAuthenticate);
} else {
print('No biometrics');
}Solution
Step 1: Check biometric availability
The code checks if biometrics are available withcanCheckBiometrics. If true, it proceeds.Step 2: Authenticate and print result
If authentication succeeds,didAuthenticateis true and printed.Final Answer:
true -> Option CQuick Check:
Available and success = true [OK]
- Assuming it prints 'No biometrics' when available
- Expecting false on success
- Ignoring await causing errors
final didAuthenticate = await auth.authenticate(
localizedReason: 'Scan fingerprint',
useErrorDialogs: true,
stickyAuth: true
);
if (didAuthenticate) {
print('Access granted');
}Solution
Step 1: Review biometric availability check
The code callsauthenticatewithout checkingcanCheckBiometricsfirst, which may cause errors on devices without biometrics.Step 2: Validate other parts
Await is present, parameter names are correct, and else block is optional.Final Answer:
No check if biometrics are available before authenticate -> Option AQuick Check:
Always check availability before authenticate [OK]
- Skipping availability check
- Confusing parameter names
- Thinking else block is mandatory
local_auth?
A) Call authenticate directly without checking availability.
B) Check canCheckBiometrics, then call authenticate, then proceed if true.
C) Skip biometric checks and use a password instead.
D) Call authenticate and ignore its result.Solution
Step 1: Verify biometric availability
UsecanCheckBiometricsto ensure device supports biometrics before authenticating.Step 2: Authenticate and check result
Callauthenticateand proceed only if it returns true, ensuring secure access.Step 3: Avoid skipping checks or ignoring results
Skipping availability or ignoring authentication result risks security.Final Answer:
Check canCheckBiometrics, then authenticate, then proceed if true -> Option BQuick Check:
Availability check + authenticate success = secure access [OK]
- Calling authenticate without availability check
- Ignoring authentication result
- Using password instead of biometrics when asked
Ready to prove it?
Pass the test and this pattern moves unknown
