Skip to content

Instantly share code, notes, and snippets.

@akifarhan
Last active May 8, 2024 01:03
Show Gist options
  • Save akifarhan/f70a2c777651f2ea61a15eb92a5939c1 to your computer and use it in GitHub Desktop.
Save akifarhan/f70a2c777651f2ea61a15eb92a5939c1 to your computer and use it in GitHub Desktop.
Flutter Local_Auth Setting for Android in Kotlin

Note that local_auth plugin requires the use of a FragmentActivity as opposed to 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).

1. Add USE_FINGERPRINT permission

  • Open android>app>src>main>AndroidManifest.xml
  • Add the permission
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.[your.package]">
  <uses-permission android:name="android.permission.USE_FINGERPRINT"/>
  <application...
    ...
    ...
    ...
<manifest>

2. Change FlutterActivity to FlutterFragmentActivity

If you receive this error: Exception has occurred. PlatformException (PlatformException(no_fragment_activity, local_auth plugin requires activity to be a FragmentActivity., null))

Then you need to do this step. Change FlutterActivity to FlutterFragmentActivity in MainActivity.kt.

package com.[your.package]

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

3. Theme.AppCompat issue

If you receive this error: Exception has occurred. PlatformException (PlatformException(error, You need to use a Theme.AppCompat theme (or descendant) with this activity., null)) Then you need to do this:

  1. Go to android>app>src>main>res>values>style.xml
  2. Change the <style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar"> to <style name="LaunchTheme" parent="Theme.AppCompat.Light.NoActionBar">

ps: The local auth would not working if you update the Android Gradle Plugin.

@akifarhan
Copy link
Author

@adminant I've faced this kind of issue before and found that the build Gradle version might be the culprit.

flutter/flutter#64996 (comment)

@manudevcode
Copy link

In my case throws 'Cannot resolve symbol 'Theme.AppCompat.Light.NoActionBar''

@vasilich6107
Copy link

API 27 app crashes on trying to call biometrics with error

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

@karun7607
Copy link

thank you so much its working for me 100%

@MoeAlshidi
Copy link

WORKED 100%

@msisinni
Copy link

msisinni commented Aug 3, 2023

On the off chance that someone is still having issues after switching to a Theme.AppCompat, try also adding android:theme="@style/LaunchTheme" to your <application ... >

    <application
        android:name="${applicationName}"
        android:theme="@style/LaunchTheme"
        ...
       >

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment