<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by M S Karthik on Medium]]></title>
        <description><![CDATA[Stories by M S Karthik on Medium]]></description>
        <link>https://medium.com/@karthimanisindhu?source=rss-0a1edafa430e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*BYRuL-FCOc-WXqJS</url>
            <title>Stories by M S Karthik on Medium</title>
            <link>https://medium.com/@karthimanisindhu?source=rss-0a1edafa430e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 19:53:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@karthimanisindhu/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Native Biometric Authentication in Flutter for Android — No Third-Party Packages]]></title>
            <link>https://karthimanisindhu.medium.com/native-biometric-authentication-in-flutter-for-android-no-third-party-packages-84a1415ca285?source=rss-0a1edafa430e------2</link>
            <guid isPermaLink="false">https://medium.com/p/84a1415ca285</guid>
            <dc:creator><![CDATA[M S Karthik]]></dc:creator>
            <pubDate>Wed, 16 Apr 2025 11:37:49 GMT</pubDate>
            <atom:updated>2025-04-16T11:55:16.981Z</atom:updated>
            <content:encoded><![CDATA[<h3>Native Biometric Authentication in Flutter for Android - No Third-Party Packages</h3><p>Biometric authentication (like <strong>Fingerprint</strong> or <strong>Face Unlock</strong>) adds a layer of convenience and security to mobile apps. While the <a href="https://pub.dev/packages/local_auth">local_auth </a>Flutter plugin is the go-to solution, there are valid reasons to avoid third-party packages:</p><h3>🧠 Why Go Native?</h3><p>Going native gives you several advantages:</p><ul><li><strong>Full control</strong> over the authentication flow</li><li><strong>Customizable UI and behavior</strong></li><li><strong>No reliance on external packages</strong>, which reduces app bloat and long-term maintenance</li><li>Ability to choose specific authentication types like fingerprint, face recognition, or device credentials</li></ul><blockquote><em>🔐 </em>By using native integration, you gain full control over the biometric prompt — including customization of UI elements and selecting specific authentication types like fingerprint, face recognition, or device credentials.</blockquote><p>In this post, I’ll walk you through how to implement <strong>native biometric authentication in Flutter for Android</strong> using Kotlin and Platform Channels -<strong>without any plugins</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*YY73-1pAIYyJYjVzFJaY8Q.png" /></figure><h3>🛠 Step 1: Flutter Side — Set Up Platform Channels</h3><p>Add at the top of the file:</p><pre>import &#39;package:flutter/services.dart&#39;;</pre><p>Let’s create a Flutter page that communicates with Android through a MethodChannel.</p><p>define a Method Channel:</p><pre>static const platform = MethodChannel(&#39;com.example.app/fingerprint&#39;);</pre><p>Before triggering the authentication prompt, we first check if the device supports fingerprint authentication using the native BiometricManager.</p><pre>bool _isFingerprintAvailable = false;<br>String _authStatus = &#39;Not Authenticated&#39;;<br><br>@override<br>void initState() {<br>  super.initState();<br>  _checkFingerprintAvailability();<br>}<br><br>Future&lt;void&gt; _checkFingerprintAvailability() async {<br>  bool isAvailable = false;<br>  try {<br>    isAvailable = await platform.invokeMethod(&#39;isFingerprintAvailable&#39;);<br>  } on PlatformException catch (e) {<br>    print(&quot;Failed to check fingerprint: ${e.message}&quot;);<br>  }<br><br>  setState(() {<br>    _isFingerprintAvailable = isAvailable;<br>  });<br>}</pre><p>Once we confirm that fingerprint authentication is available, we invoke the native BiometricPrompt to display the system biometric dialog and handle the authentication flow.</p><pre>Future&lt;void&gt; _authenticateWithFingerprint() async {<br>  if (!_isFingerprintAvailable) {<br>    setState(() {<br>      _authStatus = &#39;Fingerprint not available on this device&#39;;<br>    });<br>    return;<br>  }<br><br>  try {<br>    final bool result = await platform.invokeMethod(<br>      &#39;authenticateWithFingerprint&#39;,<br>    );<br>    setState(() {<br>      _authStatus =<br>          result ? &#39;Authentication successful&#39; : &#39;Authentication failed&#39;;<br>    });<br>  } on PlatformException catch (e) {<br>    setState(() {<br>      _authStatus = &#39;Error: ${e.message}&#39;;<br>    });<br>  }<br>}</pre><h3>🤖 Step 2: Android Side — Native Fingerprint Auth with Kotlin</h3><p>To handle fingerprint scanning natively, we’ll use Android’s BiometricPrompt and BiometricManager. Here’s how to configure MainActivity.kt file located in the kotlin folder.</p><p>Add the necessary imports at the top of the file:</p><pre>import androidx.annotation.NonNull<br>import io.flutter.embedding.android.FlutterFragmentActivity<br>import io.flutter.embedding.engine.FlutterEngine<br>import io.flutter.plugin.common.MethodChannel<br>import androidx.biometric.BiometricManager<br>import androidx.biometric.BiometricPrompt<br>import androidx.core.content.ContextCompat</pre><p>Then,</p><ul><li><strong>Setting Up the Method Channel:</strong> The configureFlutterEngine method sets up a MethodChannel to enable communication between Flutter and native Android code.</li><li><strong>Handling Method Calls:</strong> The setupMethodChannel function listens for method calls from Flutter and calls the appropriate native methods for checking fingerprint availability or starting authentication.</li><li><strong>Checking Fingerprint Availability:</strong> The checkBiometricAvailability method uses BiometricManager to check if the device supports fingerprint authentication and sends the result back to Flutter.</li><li><strong>Displaying the Authentication Prompt:</strong> The showBiometricPrompt method creates a native biometric prompt with a title, subtitle, and cancel button, and triggers the authentication process.</li><li><strong>Authentication Callback:</strong> The BiometricPrompt.AuthenticationCallback handles success and error events, sending the result back to Flutter based on whether authentication succeeds or fails.</li></ul><pre>class MainActivity: FlutterFragmentActivity() {<br>    private val channel = &quot;com.example.app/fingerprint&quot;<br>    private lateinit var biometricPrompt: BiometricPrompt<br><br>    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {<br>        super.configureFlutterEngine(flutterEngine)<br>        setupMethodChannel(flutterEngine)<br>    }<br><br>    private fun setupMethodChannel(flutterEngine: FlutterEngine) {<br>        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, channel).setMethodCallHandler { call, result -&gt;<br>            when (call.method) {<br>                &quot;isFingerprintAvailable&quot; -&gt; checkBiometricAvailability(result)<br>                &quot;authenticateWithFingerprint&quot; -&gt; showBiometricPrompt(result)<br>                else -&gt; result.notImplemented()<br>            }<br>        }<br>    }<br><br>    private fun checkBiometricAvailability(result: MethodChannel.Result) {<br>        val canAuthenticate = BiometricManager.from(this)<br>            .canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_STRONG)<br>        result.success(canAuthenticate == BiometricManager.BIOMETRIC_SUCCESS)<br>    }<br><br>    private fun showBiometricPrompt(result: MethodChannel.Result) {<br>        val executor = ContextCompat.getMainExecutor(this)<br><br>        biometricPrompt = BiometricPrompt(this, executor,<br>            object : BiometricPrompt.AuthenticationCallback() {<br>                override fun onAuthenticationSucceeded(authResult: BiometricPrompt.AuthenticationResult) {<br>                    result.success(true)<br>                }<br>                override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {<br>                    result.error(&quot;AUTHENTICATION_ERROR&quot;, errString.toString(), null)<br>                }<br>            })<br><br>        val promptInfo = BiometricPrompt.PromptInfo.Builder()<br>            .setTitle(&quot;Authenticate&quot;)<br>            .setSubtitle(&quot;Confirm your identity&quot;)<br>            .setNegativeButtonText(&quot;Cancel&quot;)<br>            .setAllowedAuthenticators(BiometricManager.Authenticators.BIOMETRIC_STRONG)<br>            .build()<br><br>        biometricPrompt.authenticate(promptInfo)<br>    }<br>}</pre><p>The UI is updated to reflect the current status, showing whether the fingerprint authentication is available and displaying the result of the authentication attempt.</p><pre>@override<br>Widget build(BuildContext context) {<br>  return Scaffold(<br>    appBar: AppBar(title: const Text(&#39;Native Fingerprint Auth&#39;)),<br>    body: Center(<br>      child: Column(<br>        mainAxisAlignment: MainAxisAlignment.center,<br>        children: &lt;Widget&gt;[<br>          Text(&#39;Fingerprint available: $_isFingerprintAvailable&#39;),<br>          const SizedBox(height: 20),<br>          Text(<br>            &#39;Status: $_authStatus&#39;,<br>            style: const TextStyle(fontWeight: FontWeight.bold),<br>          ),<br>          const SizedBox(height: 30),<br>          ElevatedButton(<br>            onPressed: _authenticateWithFingerprint,<br>            child: const Text(&#39;Authenticate with Fingerprint&#39;),<br>          ),<br>        ],<br>      ),<br>    ),<br>  );<br>}</pre><h3>📜 Step 3: Android Permissions and Dependencies</h3><p>In your AndroidManifest.xml, add the biometric permission:</p><pre>&lt;uses-permission android:name=&quot;android.permission.USE_BIOMETRIC&quot; /&gt;</pre><blockquote><em>You don’t need </em><em>USE_FINGERPRINT for modern APIs using </em><em>BiometricPrompt.</em></blockquote><p>Then, in your build.gradle (app level), include the biometric dependency:</p><pre>dependencies {<br>    implementation(&quot;androidx.biometric:biometric:1.1.0&quot;)<br>}</pre><p>Now <strong>rebuild</strong> the App and you are done ✅ 🥳</p><h3>💡 Bonus Tips</h3><ul><li>To support <strong>face unlock</strong> or <strong>device PIN</strong>, change the allowed authenticators to include BIOMETRIC_WEAK or DEVICE_CREDENTIAL.</li><li>For better UX, customize titles, subtitles, and negative button text in the native prompt.</li><li>You can also track different error codes like cancellation or hardware issues via onAuthenticationError.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*dSG6k3Tvdf3g2oaO74IlLw.gif" /></figure><p>If you found this helpful, don’t forget to leave a 👏 and share it with your fellow devs!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=84a1415ca285" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>