Biometric authentication lets users unlock apps using their fingerprint or face. It makes apps safer and easier to use without typing passwords.
0
0
Biometric authentication in Flutter
Introduction
When you want users to log in quickly without typing passwords.
To add extra security for sensitive information in your app.
When you want to replace PIN codes with fingerprint or face recognition.
For apps that require fast and secure user verification.
To improve user experience by using device biometrics.
Syntax
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.
Examples
Check if the device supports biometrics before asking the user.
Flutter
final LocalAuthentication auth = LocalAuthentication();
bool canCheck = await auth.canCheckBiometrics;Ask the user to authenticate with biometrics with a custom message.
Flutter
bool authenticated = await auth.authenticate(
localizedReason: 'Scan your fingerprint to continue',
);Use options to restrict to biometrics and keep authentication active if app goes to background.
Flutter
bool authenticated = await auth.authenticate( localizedReason: 'Use face or fingerprint', options: const AuthenticationOptions( biometricOnly: true, stickyAuth: true, ), );
Sample App
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.
Flutter
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'), ), ], ), ), ); } }
OutputSuccess
Important Notes
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.
Summary
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.