In this blog, we'll cover how to use Flutter Local Auth to implement biometric authentication on Android and iOS devices. Flutter Local Auth is a Flutter plugin that provides an easy-to-use interface to access the biometric authentication capabilities of a device.
Step 1: Add the Flutter Local Auth dependency
The first step is to add the Flutter Local Auth dependency to your project. To do this, add the following line to your pubspec.yaml file:
dependencies:
flutter_local_auth: ^2.1.6
This will download and install the latest version of Flutter Local Auth.
Step 2: Import the Flutter Local Auth package
After adding the dependency, you need to import the Flutter Local Auth package in your Dart code:
import 'package:flutter_local_auth/flutter_local_auth.dart';
Step 3: Check for biometric availability
Before you can use biometric authentication, you need to check whether the device supports it. You can do this by creating an instance of the FlutterLocalAuth class and calling the canCheckBiometrics method:
FlutterLocalAuth localAuth = FlutterLocalAuth();
Future<void> checkBiometrics() async {
bool canCheckBiometrics = await localAuth.canCheckBiometrics;
print('Biometrics available: $canCheckBiometrics');
}
The canCheckBiometrics method returns a boolean value indicating whether biometric authentication is available on the device. You can use this information to show or hide biometric authentication UI elements in your app.
Step 4: Authenticate using biometrics
To authenticate using biometrics, you need to call the authenticateWithBiometrics method of the FlutterLocalAuth class:
Future<void> authenticate() async {
bool authenticated = await
localAuth.authenticateWithBiometrics(
localizedReason:
'Please authenticate to access your account',
useErrorDialogs: true,
stickyAuth: false,
);
print('Authenticated: $authenticated');
}
The authenticateWithBiometrics method takes three optional parameters:
localizedReason:
A localized message that explains why biometric authentication is needed.
useErrorDialogs:
A boolean value that determines whether to show an error dialog if authentication fails.
stickyAuth:
A boolean value that determines whether biometric authentication is required for every app launch or only once per device unlock.
The method returns a boolean value indicating whether authentication was successful.
Step 5: Putting it all together
Now that you know how to check for biometric availability and authenticate using biometrics, you can put it all together in a complete example:
import 'package:flutter/material.dart';
import 'package:flutter_local_auth/flutter_local_auth.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Local Auth Demo',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
FlutterLocalAuth localAuth = FlutterLocalAuth();
Future<void> checkBiometrics() async {
bool canCheckBiometrics = await localAuth.canCheckBiometrics;
print('Biometrics available: $canCheckBiometrics');
}
Future<void> authenticate() async {
bool authenticated = await localAuth.authenticateWithBiometrics(
localizedReason: 'Please authenticate to access your account',
useErrorDialogs: true,
stickyAuth: false,
);
print('Authenticated: $authenticated');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter Solution Demo of local Auth'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
child: Text('Check biometrics'),
onPressed: checkBiometrics,
),
SizedBox(height: 16),
ElevatedButton(
child: Text('Authenticate with biometrics'),
onPressed: authenticate,
),
],
),
),
);
}
}
In this example, we create a simple app with two buttons: one to check whether biometric authentication is available and another to authenticate using biometrics. When the buttons are pressed, we call the checkBiometrics and authenticate methods, respectively.
The checkBiometrics method checks if biometric authentication is available on the user's device. If it is available, it logs a message to the console indicating that biometrics are available. If it is not available, it logs a message indicating that biometrics are not available.
The authenticate method attempts to authenticate the user using biometrics. It presents the user with a dialog that prompts them to authenticate using their biometric information (e.g. fingerprint, face scan). If the user successfully authenticates, the method logs a message indicating that the user was authenticated. If the user is unable to authenticate or cancels the authentication process, the method logs a message indicating that authentication failed.
By using Flutter Local Auth to implement biometric authentication in your Flutter app, you can provide your users with a secure and convenient way to access their account while also ensuring that their sensitive information remains protected.
Thankyou
- Nachiketa Pandey
0 Comments