Introduction

In this article, we are talking about Fingerprint Authentication. So far, we have discussed the authentications like Google, Facebook, 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.
  1. dependencies:
  2. flutter:
  3. sdk: flutter
  4. cupertino_icons: ^0.1.2
  5. 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.
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  2. package="com.example.app">
  3. <uses-permission android:name="android.permission.USE_FINGERPRINT"/>
  4. <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.
  1. import 'package:flutter/material.dart';
  2. //1. imported local authentication plugin
  3. import 'package:local_auth/local_auth.dart';
  4. void main() => runApp(MyApp());
  5. class MyApp extends StatelessWidget {
  6. @override
  7. Widget build(BuildContext context) {
  8. return MaterialApp(
  9. theme: ThemeData(
  10. primarySwatch: Colors.blue,
  11. ),
  12. home: MyHomePage(title: 'Fingerprint Authentication'),
  13. );
  14. }
  15. }
  16. class MyHomePage extends StatefulWidget {
  17. MyHomePage({Key key, this.title}) : super(key: key);
  18. final String title;
  19. @override
  20. _MyHomePageState createState() => _MyHomePageState();
  21. }
  22. class _MyHomePageState extends State<MyHomePage> {
  23. // 2. created object of localauthentication class
  24. final LocalAuthentication _localAuthentication = LocalAuthentication();
  25. // 3. variable for track whether your device support local authentication means
  26. // have fingerprint or face recognization sensor or not
  27. bool _hasFingerPrintSupport = false;
  28. // 4. we will set state whether user authorized or not
  29. String _authorizedOrNot = "Not Authorized";
  30. // 5. list of avalable biometric authentication supports of your device will be saved in this array
  31. List<BiometricType> _availableBuimetricType = List<BiometricType>();
  32. Future<void> _getBiometricsSupport() async {
  33. // 6. this method checks whether your device has biometric support or not
  34. bool hasFingerPrintSupport = false;
  35. try {
  36. hasFingerPrintSupport = await _localAuthentication.canCheckBiometrics;
  37. } catch (e) {
  38. print(e);
  39. }
  40. if (!mounted) return;
  41. setState(() {
  42. _hasFingerPrintSupport = hasFingerPrintSupport;
  43. });
  44. }
  45. Future<void> _getAvailableSupport() async {
  46. // 7. this method fetches all the available biometric supports of the device
  47. List<BiometricType> availableBuimetricType = List<BiometricType>();
  48. try {
  49. availableBuimetricType =
  50. await _localAuthentication.getAvailableBiometrics();
  51. } catch (e) {
  52. print(e);
  53. }
  54. if (!mounted) return;
  55. setState(() {
  56. _availableBuimetricType = availableBuimetricType;
  57. });
  58. }
  59. Future<void> _authenticateMe() async {
  60. // 8. this method opens a dialog for fingerprint authentication.
  61. // we do not need to create a dialog nut it popsup from device natively.
  62. bool authenticated = false;
  63. try {
  64. authenticated = await _localAuthentication.authenticateWithBiometrics(
  65. localizedReason: "Authenticate for Testing", // message for dialog
  66. useErrorDialogs: true,// show error in dialog
  67. stickyAuth: true,// native process
  68. );
  69. } catch (e) {
  70. print(e);
  71. }
  72. if (!mounted) return;
  73. setState(() {
  74. _authorizedOrNot = authenticated ? "Authorized" : "Not Authorized";
  75. });
  76. }
  77. @override
  78. void initState() {
  79. _getBiometricsSupport();
  80. _getAvailableSupport();
  81. super.initState();
  82. }
  83. @override
  84. Widget build(BuildContext context) {
  85. return Scaffold(
  86. appBar: AppBar(
  87. title: Text(widget.title),
  88. ),
  89. body: Center(
  90. child: Column(
  91. mainAxisAlignment: MainAxisAlignment.center,
  92. children: <Widget>[
  93. Text("Has FingerPrint Support : $_hasFingerPrintSupport"),
  94. Text(
  95. "List of Biometrics Support: ${_availableBuimetricType.toString()}"),
  96. Text("Authorized : $_authorizedOrNot"),
  97. RaisedButton(
  98. child: Text("Authorize Now"),
  99. color: Colors.green,
  100. onPressed: _authenticateMe,
  101. ),
  102. ],
  103. ),
  104. ),
  105. );
  106. }
  107. }
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.
GitHub Repo - https://github.com/myvsparth/flutter_fingerprint_auth

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.