简体   繁体   English

在 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

Package used https://pub.dev/packages/local_auth/versions Code implemented exactly as in the example. Package 使用了 https://pub.dev/packages/local_auth/versions代码实现与示例中完全相同。 https://pub.dev/packages/local_auth#-example-tab- https://pub.dev/packages/local_auth#-example-tab-

Error Description: I/flutter ( 7978): PlatformException(no_fragment_activity, local_auth plugin requires activity to be a FragmentActivity., null)错误描述:I/flutter(7978):PlatformException(no_fragment_activity,local_auth 插件要求活动为 FragmentActivity。,null)

Here is the screen shot of final result.这是最终结果的屏幕截图。 Error occurs on pressing the third button "Authenticate".按第三个按钮“验证”时发生错误。 It changes to "Cancel", and throws the above error to the console for "no_fragment_activity".它更改为“取消”,并将上述错误抛出到“no_fragment_activity”的控制台。

Example code screen示例代码屏幕

I've just built the local_auth sample and it works (vers. 0.6.3.4).我刚刚构建了 local_auth 示例,它可以工作(版本 0.6.3.4)。 Here after the steps I've done:在我完成的步骤之后:

  1. create a new flutter app:创建一个新的 flutter 应用程序:

    flutter create -a java -t app --project-name mywallet mywallet flutter create -a java -t app --project-name mywallet mywallet

Note, I'm using the java language.注意,我使用的是 java 语言。

  1. add the dependency to local_auth:将依赖项添加到 local_auth:
dependencies:
  local_auth: ^0.6.3+4
  1. copy the main.dart class from the: local_auth page从以下位置复制 main.dart class: local_auth 页面

  2. modify the MainActivity.java in this manner:以这种方式修改 MainActivity.java:

MainActivity.java MainActivity.java

package com.example.mywallet;
import android.os.Bundle;
import io.flutter.app.FlutterFragmentActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;
import android.os.Bundle; 
import io.flutter.app.FlutterFragmentActivity;
import io.flutter.plugins.flutter_plugin_android_lifecycle.FlutterAndroidLifecyclePlugin;
import io.flutter.plugins.localauth.LocalAuthPlugin;

@SuppressWarnings("deprecation")
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"));
    

  }
}

Then, modify the android manifest in order to allow the flutter fragment to start:然后,修改 android 清单以允许 flutter 片段启动:

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

    <application android:name="io.flutter.app.FlutterApplication" android:label="local_auth_example" android:icon="@mipmap/ic_launcher">
      <activity android:name="io.flutter.embedding.android.FlutterFragmentActivity"
                android:launchMode="singleTop"
                android:theme="@style/Theme.AppCompat.Light"
                android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection"
                android:hardwareAccelerated="true"
                android:windowSoftInputMode="adjustResize">
          <intent-filter>
              <action android:name="android.intent.action.MAIN"/>
              <category android:name="android.intent.category.LAUNCHER"/>
          </intent-filter>
      </activity>
      <activity
          android:name=".MainActivity"
          android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale"
          android:hardwareAccelerated="true"
          android:windowSoftInputMode="adjustResize">
      </activity>
      <meta-data android:name="flutterEmbedding" android:value="2"/>
  </application>
</manifest>

Now, the app is working if you are building it in debug mode, unfortunately in release mode it will take the following exception:现在,如果您在调试模式下构建应用程序,则该应用程序正在运行,不幸的是,在发布模式下,它将出现以下异常:

E/AndroidRuntime(27487): FATAL EXCEPTION: main
E/AndroidRuntime(27487): Process: com.example.mywallet, PID: 27487
E/AndroidRuntime(27487): java.lang.IllegalAccessError: Interface androidx.lifecycle.b implemented by class io.flutter.plugins.localauth.AuthenticationHelper is inaccessible (declaration of 'io.flutter.plugins.localauth.AuthenticationHelper' appears in /data/app/com.example.mywallet-t-m1EbOiZo85yTCgTK9tWw==/base.apk)
E/AndroidRuntime(27487):        at io.flutter.plugins.GeneratedPluginRegistrant.registerWith(Unknown Source:16)
E/AndroidRuntime(27487):        at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(27487):        at io.flutter.embedding.engine.h.g.a.a(Unknown Source:25)
E/AndroidRuntime(27487):        at io.flutter.embedding.android.FlutterFragmentActivity.g(Unknown Source:0)

In order to avoid this issue, you can add the file:为了避免这个问题,您可以添加文件:

proguard-rules.pro

in android/app/src with the following line:在 android/app/src 中使用以下行:

  -keep class androidx.lifecycle.DefaultLifecycleObserver

Now it works!现在它起作用了!

Here after my flutter doctor:在我的 flutter 医生之后:

~/Projects/mywallet$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel master, 1.25.0-5.0.pre.93, on Linux, locale it_IT.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.0-rc4)
[✓] Chrome - develop for the web
[✓] Linux toolchain - develop for Linux desktop
[✓] Android Studio (version 4.0)
[✓] IntelliJ IDEA Community Edition (version 2020.2)
[✓] VS Code (version 1.51.1)
[✓] Connected device (4 available)

• No issues found!

The simple app was tested on Samsung (with knox) in release mode and on the emulator:这个简单的应用程序在三星(使用 knox)上以发布模式和模拟器进行了测试:

~/Projects/mywallet$ flutter devices
4 connected devices:

SM A307FN (mobile)                 • RF8M9278QSF   • android-arm64  • Android 10 (API 29)
Android SDK built for x86 (mobile) • emulator-5554 • android-x86    • Android 10 (API 29) (emulator)
Linux (desktop)                    • linux         • linux-x64      • Linux
Chrome (web)                       • chrome        • web-javascript • Google Chrome 87.0.4280.88

You just forgot to change from FlutterActivity to FlutterFragmentActivity .您只是忘记从FlutterActivity更改为FlutterFragmentActivity

This step is present at the README of local_auth package, in the section Android Integration此步骤出现在local_auth package 的自述文件中,在Android Integration部分中

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)。

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

相关问题 Flutter,我使用 local_auth 0.6.3+2 进行 Touch ID、Face ID 身份验证。 但找不到如何打开输入密码按钮 - Flutter, I am using local_auth 0.6.3+2 for Touch ID, Face ID authentication. but can not find how to turn on Enter Passcode button Local_Auth FLUTTER PACKAGE - Local_Auth FLUTTER PACKAGE Flutter:local_auth 可以通过按后退按钮绕过 - Flutter: local_auth can be bypassed by pressing the back button 检测 local_auth Flutter 何时发生生物特征变化 - Detect when there is a biometric change in local_auth Flutter 我正在使用 Flutter swiper package 我想实现以下活动 - I am using Flutter swiper package and I want to achieve the following activity 在 Flutter 中配置 local_auth 插件 - configure local_auth plugin in flutter 为local_auth Flutter添加自定义对话框 - Adding custom dialog for local_auth Flutter 我想知道在Flutter中使用内网时如何使用pub get和package下载 - I am wondering how to use pub get and package download when using the internal network in Flutter 在 flutter 上运行构建运行程序时,出现以下错误 - When running build runner on flutter I get the following error 我从 flutter 中的 package“aad_auth”收到“读取初始化”错误 - I am getting a "reading init" error from the package "aad_auth" in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM