Unable to authenticate using Flutter local_auth plugin

1k Views Asked by At

I am using the Flutter local_auth plugin, biometrics works fine but Pincode/pattern doesn't provide authentication. I found if I remove my fingerprints from my mobile then Pincode and pattern authentication works but I need to input 2 times. The library seems correct but couldn't get a proper hold on the reason for this strange behaviour. Can anyone suggest?

Moreover, can we use a custom UI for the authentication, like how it is in WhatsApp?

local_auth_api.dart:

import 'package:flutter/services.dart';
import 'package:local_auth/local_auth.dart';
import 'package:local_auth/auth_strings.dart';

class LocalAuthApi {
  static final _auth = LocalAuthentication();
  
  static const iosStrings = IOSAuthMessages(
    cancelButton: 'cancel',
    goToSettingsButton: 'settings',
    goToSettingsDescription: 'Please set up your Touch ID.',
    lockOut: 'Please reenable your Touch ID');

  static const androidStrings = AndroidAuthMessages(
    cancelButton: 'cancel',
    goToSettingsButton: 'settings',
    goToSettingsDescription: 'Please set up your Touch ID.',
    signInTitle: 'User Authorization Required',
  );
    
  static Future<bool> hasBiometrics() async {
    try {
      return await _auth.canCheckBiometrics;
    } on PlatformException catch (e) {
      return false;
    }
  }

  static Future<List<BiometricType>> getBiometrics() async {
    try {
      return await _auth.getAvailableBiometrics();
    } on PlatformException catch (e) {
      return <BiometricType>[];
    }
  }

  static Future<bool> authenticate() async {
    try {
      return await _auth.authenticate(
        localizedReason: 'Scan your Fingerprint to Authenticate',
        useErrorDialogs: true,
        sensitiveTransaction: true,
        stickyAuth: true,
        iOSAuthStrings: iosStrings,
        androidAuthStrings: androidStrings
      );
    } on PlatformException catch (e) {
      print(e);
      return false;
    }
  }
}

lock_screen.dart

import 'package:xyz/resources/constants.dart';
import 'package:xyz/src/services/local_auth_api.dart';
import 'package:flutter/material.dart';

class LockScreen extends StatefulWidget{
  const LockScreen({Key? key}) : super(key: key);

  @override
  _LockScreenState createState() => _LockScreenState();
}

class _LockScreenState extends State<LockScreen>{
  @override
  void initState() {
    super.initState();
    authenticate();
  }

  authenticate() async {
    bool isAuthenticated = false;
    while(true){
      isAuthenticated = await LocalAuthApi.authenticate();
      if (isAuthenticated) {
        print(isAuthenticated);
        break;
      }
    }
    print("unlocked");
  }

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    double screenHeight = size.height;
    double screenWidth = size.width;
    return Scaffold(
      backgroundColor: primaryColor,
      body: Center(
      )
    );
  }
}

Response:

E/BiometricFragment(19568): Not launching prompt. Client activity was null.
0

There are 0 best solutions below