Introduction

Facebook, In this article, we are talking about Fingerprint Authentication. So far, we have discussed the authentications like Google Phone and email/password that will need authentication at the server-side. Fingerprint Authentication is a type of local authentication with which you can include other biometric authentication also, like face and voice recognition. We are only covering Fingerprint Authentication in this article. So, tie your seat belt and let’s start :)))

I have found a plugin, named "local_auth" for local authentication and we are going to use it for Fingerprint Authentication.

Prerequisites

The fingerprint sensor in the mobile device you are testing the application on.

Steps

Step 1

Create a new Flutter project. I have created a new project named “flutter_fingerprint_auth”.

Step 2

Add a dependency for the “local_auth” plugin in the “pubspec.yaml” file which is available in the project root directory.

dependencies: flutter: sdk: flutter cupertino_icons: ^0.1.2 local_auth: ^0.4.0+1

NOTE

I have tried local_auth version 0.5.2+3 but it is not working; however, version 0.4.0+1 is working perfectly fine for me with Flutter packages.

Step 3

Add a permission for Android in android/app/src/main/AndroidManifest.xml.

<manifest xmlns:android= "http://schemas.android.com/apk/res/android" package= "com.example.app" > <uses-permission android:name= "android.permission.USE_FINGERPRINT" /> <manifest>

Step 4

I have put some main functions for understanding the whole authentication process. Please read the comments in the code which explain you the process. I have also given my GitHub project directory link below.

import 'package:flutter/material.dart' ; import 'package:local_auth/local_auth.dart' ; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @ override Widget build(BuildContext context) { return MaterialApp( theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(title: 'Fingerprint Authentication' ), ); } } class MyHomePage extends StatefulWidget { MyHomePage({Key key, this .title}) : super(key: key); final String title; @ override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { final LocalAuthentication _localAuthentication = LocalAuthentication(); bool _hasFingerPrintSupport = false ; String _authorizedOrNot = "Not Authorized" ; List<BiometricType> _availableBuimetricType = List<BiometricType>(); Future< void > _getBiometricsSupport() async { bool hasFingerPrintSupport = false ; try { hasFingerPrintSupport = await _localAuthentication.canCheckBiometrics; } catch (e) { print(e); } if (!mounted) return ; setState(() { _hasFingerPrintSupport = hasFingerPrintSupport; }); } Future< void > _getAvailableSupport() async { List<BiometricType> availableBuimetricType = List<BiometricType>(); try { availableBuimetricType = await _localAuthentication.getAvailableBiometrics(); } catch (e) { print(e); } if (!mounted) return ; setState(() { _availableBuimetricType = availableBuimetricType; }); } Future< void > _authenticateMe() async { bool authenticated = false ; try { authenticated = await _localAuthentication.authenticateWithBiometrics( localizedReason: "Authenticate for Testing" , useErrorDialogs: true , stickyAuth: true , ); } catch (e) { print(e); } if (!mounted) return ; setState(() { _authorizedOrNot = authenticated ? "Authorized" : "Not Authorized" ; }); } @ override void initState() { _getBiometricsSupport(); _getAvailableSupport(); super.initState(); } @ override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text( "Has FingerPrint Support : $_hasFingerPrintSupport" ), Text( "List of Biometrics Support: ${_availableBuimetricType.toString()}" ), Text( "Authorized : $_authorizedOrNot" ), RaisedButton( child: Text( "Authorize Now" ), color: Colors.green, onPressed: _authenticateMe, ), ], ), ), ); } }

Sometimes, there may arrive some error as stated below.

Error - import androidx.annotation.NonNull;

Solution

Put android.useAndroidX=true and android.enableJetifier=true in the android/gradle.properties file.

Conclusion

Thus, we learned how to use Fingerprint Authentication in an Android device using local_auth plugin in Flutter. This article was all about fingerprint authentication in Android; you can also use this for iOS but for that, you need to add permission to iOS which is defined in the plugin's documentation. You can use this authentication when you need a very high level of security like making payments or confidential transactions.