[英]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
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"));
}
}
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.