【问题标题】:flutter localauth : Required security features not enabled颤振 localauth :未启用所需的安全功能
【发布时间】:2021-04-09 19:48:04
【问题描述】:

我正在使用 local_auth: ^1.1.3 并正在使用

await _localAuthentication.authenticate(
          localizedReason: S.of(Get.context!).main_pass_view_body,
          // "Please authenticate to complete your transaction",
          androidAuthStrings: androidMessages,
          iOSAuthStrings: iosMessages,
          useErrorDialogs: true,
          stickyAuth: true);

但是当我调用authenticate 方法时出现此错误:

PlatformException(NotAvailable, Required security features not enabled, null, null) 

我希望,当用户没有在手机上配置指纹时,当点击验证按钮时,默认生物识别对话框打开并且用户可以在手机上进行配置,但我得到了那个错误。 它应该打开默认的生物识别对话框以将用户转移到手机的配置部分,但我得到了错误。

我还在清单中添加了 <uses-permission android:name="android.permission.USE_BIOMETRIC"/>

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.0.4, on Linux, locale en_US.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.0-rc1)
[✓] Chrome - develop for the web
[!] Android Studio (not installed)
[✓] VS Code (version 1.55.1)
[✓] Connected device (2 available)

! Doctor found issues in 1 category.

和颤振的sdk

environment:
  sdk: ">=2.12.0 <3.0.0"

【问题讨论】:

    标签: flutter flutter-dependencies


    【解决方案1】:

    我通过捕获PlatformException 异常并在调用authenticate() 方法之前检查canCheckBiometricsisDeviceSupported() 解决了这个问题。

    这是sn-p的代码:

    if (await auth.canCheckBiometrics && await auth.isDeviceSupported()){
        try {
            if(await auth.authenticate(
              ... // authenticate configuration
               )){
                   // authenticated
            }
          } on PlatformException catch (exception) {
            // catch the exception
          }
        }
    

    【讨论】:

      【解决方案2】:

      你可以试试下面的代码

      import 'dart:async';
      
      import 'package:flutter/material.dart';
      import 'package:flutter/services.dart';
      import 'package:local_auth/local_auth.dart';
      
      void main() {
        runApp(MyApp());
      }
      
      class MyApp extends StatefulWidget {
        @override
        _MyAppState createState() => _MyAppState();
      }
      
      class _MyAppState extends State<MyApp> {
        final LocalAuthentication auth = LocalAuthentication();
        _SupportState _supportState = _SupportState.unknown;
        bool? _canCheckBiometrics;
        List<BiometricType>? _availableBiometrics;
        String _authorized = 'Not Authorized';
        bool _isAuthenticating = false;
      
        @override
        void initState() {
          super.initState();
          auth.isDeviceSupported().then(
                (isSupported) => setState(() => _supportState = isSupported
                ? _SupportState.supported
                : _SupportState.unsupported),
          );
        }
      
        Future<void> _checkBiometrics() async {
          late bool canCheckBiometrics;
          try {
            canCheckBiometrics = await auth.canCheckBiometrics;
          } on PlatformException catch (e) {
            canCheckBiometrics = false;
            print(e);
          }
          if (!mounted) return;
      
          setState(() {
            _canCheckBiometrics = canCheckBiometrics;
          });
        }
      
        Future<void> _getAvailableBiometrics() async {
          late List<BiometricType> availableBiometrics;
          try {
            availableBiometrics = await auth.getAvailableBiometrics();
          } on PlatformException catch (e) {
            availableBiometrics = <BiometricType>[];
            print(e);
          }
          if (!mounted) return;
      
          setState(() {
            _availableBiometrics = availableBiometrics;
          });
        }
      
        Future<void> _authenticate() async {
          bool authenticated = false;
          try {
            setState(() {
              _isAuthenticating = true;
              _authorized = 'Authenticating';
            });
            authenticated = await auth.authenticate(
                localizedReason: 'Let OS determine authentication method',
                useErrorDialogs: true,
                stickyAuth: true);
            setState(() {
              _isAuthenticating = false;
            });
          } on PlatformException catch (e) {
            print(e);
            setState(() {
              _isAuthenticating = false;
              _authorized = "Error - ${e.message}";
            });
            return;
          }
          if (!mounted) return;
      
          setState(
                  () => _authorized = authenticated ? 'Authorized' : 'Not Authorized');
        }
      
        Future<void> _authenticateWithBiometrics() async {
          bool authenticated = false;
          try {
            setState(() {
              _isAuthenticating = true;
              _authorized = 'Authenticating';
            });
            authenticated = await auth.authenticate(
                localizedReason:
                'Scan your fingerprint (or face or whatever) to authenticate',
                useErrorDialogs: true,
                stickyAuth: true,
                biometricOnly: true);
            setState(() {
              _isAuthenticating = false;
              _authorized = 'Authenticating';
            });
          } on PlatformException catch (e) {
            print(e);
            setState(() {
              _isAuthenticating = false;
              _authorized = "Error - ${e.message}";
            });
            return;
          }
          if (!mounted) return;
      
          final String message = authenticated ? 'Authorized' : 'Not Authorized';
          setState(() {
            _authorized = message;
          });
        }
      
        void _cancelAuthentication() async {
          await auth.stopAuthentication();
          setState(() => _isAuthenticating = false);
        }
      
        @override
        Widget build(BuildContext context) {
          return MaterialApp(
            home: Scaffold(
              appBar: AppBar(
                title: const Text('Plugin example app'),
              ),
              body: ListView(
                padding: const EdgeInsets.only(top: 30),
                children: [
                  Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      if (_supportState == _SupportState.unknown)
                        CircularProgressIndicator()
                      else if (_supportState == _SupportState.supported)
                        Text("This device is supported")
                      else
                        Text("This device is not supported"),
                      Divider(height: 100),
                      Text('Can check biometrics: $_canCheckBiometrics\n'),
                      ElevatedButton(
                        child: const Text('Check biometrics'),
                        onPressed: _checkBiometrics,
                      ),
                      Divider(height: 100),
                      Text('Available biometrics: $_availableBiometrics\n'),
                      ElevatedButton(
                        child: const Text('Get available biometrics'),
                        onPressed: _getAvailableBiometrics,
                      ),
                      Divider(height: 100),
                      Text('Current State: $_authorized\n'),
                      (_isAuthenticating)
                          ? ElevatedButton(
                        onPressed: _cancelAuthentication,
                        child: Row(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Text("Cancel Authentication"),
                            Icon(Icons.cancel),
                          ],
                        ),
                      )
                          : Column(
                        children: [
                          ElevatedButton(
                            child: Row(
                              mainAxisSize: MainAxisSize.min,
                              children: [
                                Text('Authenticate'),
                                Icon(Icons.perm_device_information),
                              ],
                            ),
                            onPressed: _authenticate,
                          ),
                          ElevatedButton(
                            child: Row(
                              mainAxisSize: MainAxisSize.min,
                              children: [
                                Text(_isAuthenticating
                                    ? 'Cancel'
                                    : 'Authenticate: biometrics only'),
                                Icon(Icons.fingerprint),
                              ],
                            ),
                            onPressed: _authenticateWithBiometrics,
                          ),
                        ],
                      ),
                    ],
                  ),
                ],
              ),
            ),
          );
        }
      }
      
      enum _SupportState {
        unknown,
        supported,
        unsupported,
      }
      

      【讨论】:

        猜你喜欢
        • 2020-12-25
        • 1970-01-01
        • 2021-12-27
        • 2020-10-03
        • 2020-04-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-19
        相关资源
        最近更新 更多