← Back to list

Enabling Local Authentication for your Flutter Application.

Mobile notifications are an essential aspect of user engagement in any app. They allow apps to send important information to users in real…

Vinayak in YavarTechWorks · 2023-07-07 12:06 · 103 claps · 2.1 min read
#flutter #flutter-app-development #local-authentication #mobile-app-development
Open on Medium ↗
Wiki topics: 📱 · Mobile Development

Enabling Local Authentication for your Flutter Application.

Mobile notifications are an essential aspect of user engagement in any app. They allow apps to send important information to users in real time, keeping them informed and engaged with the app’s content. Flutter provides a simple and effective way to implement local notifications, which are notifications that are generated by the app itself rather than from a server.

In this article, we will discuss how to use Flutter to create and schedule local notifications in your mobile application.

Required Plugin

Just go to **pub.dev** and search for local_auth package.

From documentation:

local_authprovides means to perform local, on-device authentication of the user.

On supported devices, this includes authentication with biometrics such as fingerprint or facial recognition.

Install the dependency:

$ flutter pub add local_auth

Import the package:

import 'package:local_auth/local_auth.dart';

Android Integration

In android/app/src/main/AndroidManifest.xml:

Permissions:

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

This will allow an app to use device-supported biometric modalities.

In android/app/build.gradle:

defaultConfig{
 minSdkVersion 21
}

Change the minSdkVersion to 21 as it is required.

dependencies{
 implementation “com.android.support:multidex:1.0.3”
}

In android/app/src/main/kotlin/com/example/[your.package]/MainActivity.kt:

The plugin local_auth requires the use of a FragmentActivity instead of an Activity. To update your application:

package com.[your.package]

import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterFragmentActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant 
class MainActivity: FlutterFragmentActivity() 
  {
    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) 
      {
        GeneratedPluginRegistrant.registerWith(flutterEngine) 
      }
  }

Checking for biometric availability

final _auth = LocalAuthentication();
final isAvailable = await _auth.canCheckBiometrics;
final isDeviceSupported = await _auth.isDeviceSupported();

initializing LocalAuthentication

_auth = LocalAuthentication();

Checking if biometrics are available

final isAvailable = await _auth.canCheckBiometrics; //returns bool

canCheckBiometrics only indicates whether hardware support is available, not whether the device has any biometrics enrolled.

Checking if the device is supported

final isDeviceSupported = await _auth.isDeviceSupported(); //returns bool

Authentication using Biometric

try {
  authenticated = await auth.authenticateWithBiometrics(
      localizedReason: 'Touch your finger on the sensor to login');
} catch (e) {
  print("error using biometric auth: $e");
}

authenticateWithBiometrics() is a method that returns a dialog box to provide a message to the user to press the fingerprint sensor for authentication.

Full Method

import 'package:local_auth/local_auth.dart';

final _auth = LocalAuthentication();

Future<bool> hasBiometrics() async {
    final isAvailable = await _auth.canCheckBiometrics;
    final isDeviceSupported = await _auth.isDeviceSupported();
    return isAvailable && isDeviceSupported;
}

Future<bool> authenticate() async {
    final isAuthAvailable = await hasBiometrics();
    if (!isAuthAvailable) return false;
    try {
      return await _auth.authenticate(
        localizedReason: 'Touch your finger on the sensor to login');
    } catch (e) {
      return false;
    }
  }

}

In main.dart

When authenticate() is called, it checks whether the mobile has biometrics available. If not available, it will return false, or else if it returns true (ie. the Mobile device has biometrics available and the device is supported) it will ask for the fingerprint of the user.

SystemNavigator.pop() will remove this activity from the stack and return to the previous activity. In our case, it will return to the Home screen of our mobile on unsuccessful authentication.

Thanks for reading this article ❤

If I got something wrong? Let me know in the comments. I would love to improve.

Until then Bye-Bye.


메타데이터
post_id
2b26a4451b9d
slug
enabling-local-authentication-for-your-flutter-application-2b26a4451b9d
url
https://medium.com/yavar/enabling-local-authentication-for-your-flutter-application-2b26a4451b9d
canonical_url
https://medium.com/yavar/enabling-local-authentication-for-your-flutter-application-2b26a4451b9d
author_url
https://medium.com/@vinayak_amirtharaj
status
ok
fetched_at
2026-07-22 05:07:27