簡體   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 使用了 https://pub.dev/packages/local_auth/versions代碼實現與示例中完全相同。 https://pub.dev/packages/local_auth#-example-tab-

錯誤描述:I/flutter(7978):PlatformException(no_fragment_activity,local_auth 插件要求活動為 FragmentActivity。,null)

這是最終結果的屏幕截圖。 按第三個按鈕“驗證”時發生錯誤。 它更改為“取消”,並將上述錯誤拋出到“no_fragment_activity”的控制台。

示例代碼屏幕

我剛剛構建了 local_auth 示例,它可以工作(版本 0.6.3.4)。 在我完成的步驟之后:

  1. 創建一個新的 flutter 應用程序:

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

注意,我使用的是 java 語言。

  1. 將依賴項添加到 local_auth:
dependencies:
  local_auth: ^0.6.3+4
  1. 從以下位置復制 main.dart class: local_auth 頁面

  2. 以這種方式修改 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"));
    

  }
}

然后,修改 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>

現在,如果您在調試模式下構建應用程序,則該應用程序正在運行,不幸的是,在發布模式下,它將出現以下異常:

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)

為了避免這個問題,您可以添加文件:

proguard-rules.pro

在 android/app/src 中使用以下行:

  -keep class androidx.lifecycle.DefaultLifecycleObserver

現在它起作用了!

在我的 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!

這個簡單的應用程序在三星(使用 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

您只是忘記從FlutterActivity更改為FlutterFragmentActivity

此步驟出現在local_auth package 的自述文件中,在Android Integration部分中

請注意,local_auth 插件需要使用 FragmentActivity 而不是 Activity。 這可以通過在清單中切換到使用 FlutterFragmentActivity 而不是 FlutterActivity 來輕松完成(如果您正在擴展基類,則可以使用您自己的 Activity class)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM