Home > Net >  Authenticate with Biometrics not defined Flutter
Authenticate with Biometrics not defined Flutter

Time:07-16

I'm trying to add Biometrics for IOS Devices but I keep on getting this error:

The method 'authenticateWithBiometrics' isn't defined for the type 'LocalAuthentication'.

This is the code for my api file:

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

class LocalAuthApi {
  static final _auth = LocalAuthentication();

  static Future<bool> hasBiometrics() async {
    try {
      return await _auth.canCheckBiometrics;
    } on PlatformException catch (e) {
      return false;
    }
  }

  static Future<bool> authenticate() async {
    final isAvailable = await hasBiometrics();
    if (!isAvailable) return false;

    try {
      return await _auth.authenticateWithBiometrics(
        androidAuthStrings: const AndroidAuthMessages(
          signInTitle: 'Face ID Required',
        ),
        localizedReason: 'Scan Face to Authenticate',
        useErrorDialogs: false,
        stickyAuth: false,
      );
    } on PlatformException catch (e) {
      return false;
    }
  }
}

CodePudding user response:

The documentation of package: https://pub.dev/packages/local_auth/example

Just change _auth.authenticateWithBiometrics to _auth.authenticate it must be work. Then your code:

static Future<bool> authenticate() async {
    final isAvailable = await hasBiometrics();
    if (!isAvailable) return false;

    try {
      return await _auth.authenticate(
        androidAuthStrings: const AndroidAuthMessages(
          signInTitle: 'Face ID Required',
        ),
        localizedReason: 'Scan Face to Authenticate',
        options: const AuthenticationOptions(
          useErrorDialogs: true,
          stickyAuth: true,
        ),
      );
    } on PlatformException catch (e) {
      return false;
    }
  }
  • Related