简体   繁体   English

错误:使用生物特征验证时出错:PlatformException(no_fragment_activity,local_auth 插件要求活动是 FragmentActivity。,null,null)

[英]Error: error using biometric auth: PlatformException(no_fragment_activity, local_auth plugin requires activity to be a FragmentActivity., null, null)

I am trying to add fingerprint access in y flutter app but I am not able to as I am getting the following error:我正在尝试在 y flutter 应用程序中添加指纹访问权限,但我无法做到,因为我收到以下错误:

error using biometric auth: PlatformException(no_fragment_activity, local_auth plugin requires activity to be a FragmentActivity., null, null)

Following is my code:以下是我的代码:

main.dart: main.dart:

import 'package:flutter/material.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> {
  bool isAuth = false;
  void checkBiometric() async {
    final LocalAuthentication auth = LocalAuthentication();
    bool canCheckBiometrics = false;
    try {
      canCheckBiometrics = await auth.canCheckBiometrics;
    } catch (e) {
      print("error biome trics $e");
    }
    print("biometric is available: $canCheckBiometrics");
    List<BiometricType> availableBiometrics;
    try {
      availableBiometrics = await auth.getAvailableBiometrics();
    } catch (e) {
      print("error enumerate biometrics $e");
    }
    print("following biometrics are available");
    if (availableBiometrics.isNotEmpty) {
      availableBiometrics.forEach((ab) {
        print("\ttech: $ab");
      });
    } else {
      print("no biometrics are available");
    }

    bool authenticated = false;

    try {
      authenticated = await auth.authenticateWithBiometrics(
          localizedReason: 'Touch your finger on the sensor to login',
          useErrorDialogs: true,
          stickyAuth: false
        // androidAuthStrings:AndroidAuthMessages(signInTitle: "Login to HomePage")
      );
    } catch (e) {
      print("error using biometric auth: $e");
    }

    setState(() {
      isAuth = authenticated ? true : false;
    });

    print("authenticated: $authenticated");
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        debugShowCheckedModeBanner: false,
        home: new Scaffold(
          appBar: new AppBar(
            title: new Text('BioAuthentication'),
          ),
          body: new Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Expanded(
                child: Container(),
              ),
              new RaisedButton(
                splashColor: Colors.pinkAccent,
                color: Colors.black,
                child: new Text(
                  "Authentiate",
                  style: new TextStyle(fontSize: 20.0, color: Colors.white),
                ),
                onPressed: checkBiometric,
              ),
              new Expanded(
                child: Container(),
              ),
              isAuth == true
                  ? Text(
                "Authenticated",
                softWrap: true,
                style: new TextStyle(fontSize: 30.0, color: Colors.black),
              )
                  : Text(
                "Not Authenticated",
                softWrap: true,
                style: new TextStyle(fontSize: 30.0, color: Colors.black),
              ),
              new Expanded(
                child: Container(),
              ),
            ],
          ),
        ));
  }
}

MainActivity.kt: MainActivity.kt:

package com.open.flutter_fingerprint_new

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);
    }
}

Can someone help me with this please?有人可以帮我吗?

Note: Instead of using a button is there any option so that on the launch of the screen the application prompts the user to enter the fingerprint?注意:除了使用按钮之外,还有什么选项可以在屏幕启动时提示用户输入指纹?

You should carefully check Android Integration and ios Integration.https://github.com/flutter/plugins/tree/master/packages/local_auth#android-integration您应该仔细检查 Android 集成和 ios 集成。https://github.com/flutter/plugins/tree/master/packages/local_auth#android-integration

Android Integration Android 集成

Note that local_auth plugin requires the use of a FragmentActivity as opposed to Activity.请注意,local_auth 插件需要使用 FragmentActivity 而不是 Activity。 This can be easily done by switching to use FlutterFragmentActivity as opposed to FlutterActivity in your manifest (or your own Activity class if you are extending the base class).这可以通过在清单中切换到使用 FlutterFragmentActivity 而不是 FlutterActivity 来轻松完成(如果您正在扩展基类,则可以使用您自己的 Activity class)。

import android.os.Bundle;
import io.flutter.app.FlutterFragmentActivity;
import io.flutter.plugins.flutter_plugin_android_lifecycle.FlutterAndroidLifecyclePlugin;
import io.flutter.plugins.localauth.LocalAuthPlugin;

public class MainActivity extends FlutterFragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FlutterAndroidLifecyclePlugin.registerWith(
                registrarFor(
                        "io.flutter.plugins.flutter_plugin_android_lifecycle.FlutterAndroidLifecyclePlugin"));
        LocalAuthPlugin.registerWith(registrarFor("io.flutter.plugins.localauth.LocalAuthPlugin"));
    }
}

OR Update your MainActivity.kt:或更新您的 MainActivity.kt:

import io.flutter.embedding.android.FlutterFragmentActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant

class MainActivity: FlutterFragmentActivity() {
    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine)
    }
}

Changing from FlutterActivity causes other plugins that depends on that doesn't work.从 FlutterActivity 更改会导致依赖于它的其他插件不起作用。 (If someone knows why that happens, please let me know in the comments). (如果有人知道为什么会这样,请在评论中告诉我)。

This fixed that issue for me, change ".MainActivity" in android.name to "io.flutter.embedding.android.FlutterFragmentActivity".这为我解决了这个问题,将 android.name 中的“.MainActivity”更改为“io.flutter.embedding.android.FlutterFragmentActivity”。 But, doing like the below code makes us unable to customize or edit in MainActivity.但是,像下面的代码那样做使我们无法在 MainActivity 中自定义或编辑。

 <application
    ...
    <activity
        android:name="io.flutter.embedding.android.FlutterFragmentActivity" 
        android:launchMode="singleTop"
        android:theme="@style/LaunchTheme"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:PlatformException(no_fragment_activity,local_auth 插件要求活动是 FragmentActivity。,null) - Error: PlatformException(no_fragment_activity, local_auth plugin requires activity to be a FragmentActivity., null) flutter 中的 PlatformException(no_fragment_activity,local_auth 插件要求活动为 FragmentActivity。,null) - PlatformException(no_fragment_activity, local_auth plugin requires activity to be a FragmentActivity., null) in flutter Flutter : local_auth: ^0.6.1 : PlatformException(error, You need to use a Theme.AppCompat theme (or后代) with this Activity., null) - Flutter : local_auth: ^0.6.1 : PlatformException(error, You need to use a Theme.AppCompat theme (or descendant) with this activity., null) 检测 local_auth Flutter 何时发生生物特征变化 - Detect when there is a biometric change in local_auth Flutter 在 Flutter 中配置 local_auth 插件 - configure local_auth plugin in flutter Local_Auth FLUTTER PACKAGE - Local_Auth FLUTTER PACKAGE 使用 data_usage 插件时出错:未处理的异常:PlatformException(PERMISSION_NOT_GRANTED,null,null,null) - Error wile using data_usage plugin : Unhandled Exception: PlatformException(PERMISSION_NOT_GRANTED, null, null, null) local_auth iOS(扑) - local_auth iOS (Flutter) 在 Flutter 中,我使用 local_auth 0.6.2+3 package 并实现了示例代码。 单击身份验证按钮时出现以下错误 - In Flutter, I am using local_auth 0.6.2+3 package and implemented the example code. I get the following error when I click the authenticate button PlatformException(channel-error, Unable to establish connection on channel., null, null) - 保存到本地存储 - PlatformException(channel-error, Unable to establish connection on channel., null, null) - saving to local storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM