<?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 Samiran Shil on Medium]]></title>
        <description><![CDATA[Stories by Samiran Shil on Medium]]></description>
        <link>https://medium.com/@samiranpersonal?source=rss-abf2ef80171------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*ujbtW4AZj7bDcxG7</url>
            <title>Stories by Samiran Shil on Medium</title>
            <link>https://medium.com/@samiranpersonal?source=rss-abf2ef80171------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 19:59:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@samiranpersonal/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[Create an Android Plugin in Flutter]]></title>
            <link>https://medium.com/@samiranpersonal/create-an-android-plugin-in-flutter-b1df05c94bdb?source=rss-abf2ef80171------2</link>
            <guid isPermaLink="false">https://medium.com/p/b1df05c94bdb</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[flutter-plugin]]></category>
            <category><![CDATA[flutter]]></category>
            <dc:creator><![CDATA[Samiran Shil]]></dc:creator>
            <pubDate>Tue, 16 Jun 2026 21:17:07 GMT</pubDate>
            <atom:updated>2026-06-17T19:56:47.594Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*IcgFi3YYMKUpUo0D_qrM3A.png" /></figure><p>Hi everyone.</p><p>In my previous posts, I discussed Method Channels and some of their limitations. Today, we’ll take the next step and create a simple Flutter plugin that allows Dart code to determine whether Android Battery Saver mode is currently enabled.</p><h3>Creating the Plugin</h3><p>Create a new Flutter plugin using Android Studio or VS Code. I’ll be using Android Studio for this example.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/1*TJt_50ssWn9lwqM_bPHZ7w.png" /></figure><p>After creating the plugin and running flutter pub get, open the example/android module and sync Gradle. This allows Android Studio to properly index the Android project, resolve imports, and provide code completion.</p><p>The generated plugin contains three important Dart files:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*vESKonY6XvB6bstqbgwqMw.png" /></figure><p>For android kotlin we will be coding inside this file,</p><p>1. battery_saver_mode_platform_interface.dart</p><pre>abstract class BatterySaverModePlatform extends PlatformInterface {<br>  /// Constructs a BatterySaverModePlatform.<br>  BatterySaverModePlatform() : super(token: _token);<br>  static final Object _token = Object();<br>  static BatterySaverModePlatform _instance = MethodChannelBatterySaverMode();<br>  /// The default instance of [BatterySaverModePlatform] to use.<br>  ///<br>  /// Defaults to [MethodChannelBatterySaverMode].<br>  static BatterySaverModePlatform get instance =&gt; _instance;<br>  /// Platform-specific implementations should set this with their own<br>  /// platform-specific class that extends [BatterySaverModePlatform] when<br>  /// they register themselves.<br>  static set instance(BatterySaverModePlatform instance) {<br>    PlatformInterface.verifyToken(instance, _token);<br>    _instance = instance;<br>  }<br>  Future&lt;bool&gt; isPowerSaveMode() {<br>    throw UnimplementedError(&#39;isPowerSaveMode() has not been implemented.&#39;);<br>  }<br>}</pre><p>2. battery_saver_mode_method_channel.dart</p><pre>class MethodChannelBatterySaverMode extends BatterySaverModePlatform {<br>  /// The method channel used to interact with the native platform.<br>  @visibleForTesting<br>  final methodChannel = const MethodChannel(&#39;battery_saver_mode&#39;);<br><br>  @override<br>  Future&lt;bool&gt; isPowerSaveMode() async {<br>    final bool result =<br>        await methodChannel.invokeMethod&lt;bool&gt;(&#39;isPowerSaveMode&#39;) ?? false;<br>    return result;<br>  }<br>}</pre><p>3. battery_saver_mode.dart</p><pre>class BatterySaverMode {<br>  Future&lt;bool&gt; isPowerSaveMode() {<br>    return BatterySaverModePlatform.instance.isPowerSaveMode();<br>  }<br>}</pre><p>Now lets move to the Android Side,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/1*4NHIOWyAwaLyNb5yjk_gOA.png" /></figure><ol><li>BatterySaverModePlugin</li></ol><pre>class BatterySaverModePlugin :<br>    FlutterPlugin,<br>    MethodCallHandler {<br>    private lateinit var methodChannel: MethodChannel<br>    private lateinit var context: Context<br>    private var powerManager: PowerManager? = null<br>    override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {<br>        context = flutterPluginBinding.applicationContext<br>        powerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager<br>        methodChannel = MethodChannel(flutterPluginBinding.binaryMessenger, &quot;battery_saver_mode&quot;)<br>        methodChannel.setMethodCallHandler(this)<br>    }<br>    override fun onMethodCall(<br>        call: MethodCall,<br>        result: Result<br>    ) {<br>        when (call.method) {<br>            &quot;isPowerSaveMode&quot; -&gt; result.success(isPowerSaveMode())<br>            else -&gt; result.notImplemented()<br>        }<br>    }<br>    private fun isPowerSaveMode(): Boolean {<br>        return if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.LOLLIPOP) {<br>            powerManager?.isPowerSaveMode ?: false<br>        } else {<br>            false<br>        }<br>    }<br>    override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {<br>        methodChannel.setMethodCallHandler(null)<br>    }<br>}</pre><h3>Using the Plugin</h3><p>Once the plugin is added to your pubspec.yaml, you can call it from Dart:</p><pre>final isPowerSaveMode =<br>    await BatterySaverMode().isPowerSaveMode();</pre><h3>Why Create a Plugin?</h3><p>You might notice that the plugin still uses a Method Channel internally.</p><p>So what’s the difference?</p><p>The difference isn’t the communication mechanism. The difference is how the native functionality is packaged and registered.</p><p>By creating a plugin:</p><ul><li>Native functionality is encapsulated behind a clean Flutter API.</li><li>The implementation becomes reusable across multiple projects.</li><li>Testing becomes easier.</li><li>The plugin can be registered with different Flutter engines instead of being tightly coupled to a specific MainActivity.</li></ul><p>This is why many production-ready Flutter packages are implemented as plugins rather than directly registering Method Channels inside an Activity.</p><h3>Conclusion</h3><p>A Flutter plugin is often just a well-structured wrapper around Method Channels.</p><p>In this example, we used a Method Channel internally to communicate with Android’s PowerManager API and expose the result through a simple Flutter API.</p><p>Understanding how plugins are built gives you much more control when integrating native Android or iOS functionality into your Flutter applications.</p><p>Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b1df05c94bdb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Simple Method Channel Example in Flutter]]></title>
            <link>https://medium.com/@samiranpersonal/a-simple-method-channel-example-in-flutter-b02d87dc6be0?source=rss-abf2ef80171------2</link>
            <guid isPermaLink="false">https://medium.com/p/b02d87dc6be0</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[method-channel]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Samiran Shil]]></dc:creator>
            <pubDate>Mon, 08 Jun 2026 21:39:48 GMT</pubDate>
            <atom:updated>2026-06-08T21:39:48.269Z</atom:updated>
            <content:encoded><![CDATA[<p>Method Channels allow Flutter to communicate with native Android or iOS code. In this example, we’ll check whether <strong>Battery Saver Mode</strong> is enabled on an Android device.</p><h3>Flutter (Dart) Side</h3><pre>class BatteryOptimizationHelper {<br>  static const MethodChannel _channel =<br>      MethodChannel(&#39;battery_optimization_helper&#39;);<br>  static Future&lt;bool&gt; isBatterySaverEnabled() async {<br>    try {<br>      final bool result =<br>          await _channel.invokeMethod(&#39;isBatterySaverEnabled&#39;);<br>      return result;<br>    } on PlatformException {<br>      return false;<br>    }<br>  }<br>}</pre><p>Usage:</p><pre>bool isEnabled =<br>    await BatteryOptimizationHelper.isBatterySaverEnabled();</pre><h3>Android (Kotlin) Side</h3><pre>class MainActivity : FlutterFragmentActivity() {<br>    private lateinit var powerManager: PowerManager<br>    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {<br>        super.configureFlutterEngine(flutterEngine)<br>        powerManager = getSystemService(POWER_SERVICE) as PowerManager<br>        MethodChannel(<br>            flutterEngine.dartExecutor.binaryMessenger,<br>            &quot;battery_optimization_helper&quot;<br>        ).setMethodCallHandler { call, result -&gt;<br>            when (call.method) {<br>                &quot;isBatterySaverEnabled&quot; -&gt; {<br>                    result.success(powerManager.isPowerSaveMode)<br>                }<br>                else -&gt; result.notImplemented()<br>            }<br>        }<br>    }<br>}</pre><h3>How It Works</h3><ol><li>Flutter calls isBatterySaverEnabled() using a Method Channel.</li><li>The request is sent to the Android platform.</li><li>Android receives the method call and checks the value of PowerManager.isPowerSaveMode.</li><li>The result is returned back to Flutter as a boolean value.</li></ol><p>This allows a Flutter application to access Android APIs that are not directly available through Flutter itself.</p><p>Method Channels are commonly used to:</p><ul><li>Access platform-specific APIs</li><li>Integrate native SDKs</li><li>Perform custom Android or iOS operations</li><li>Extend Flutter beyond its built-in capabilities</li></ul><p>That’s it! A simple example, but the same concept can be used to communicate with almost any native functionality on Android or iOS.</p><p><strong>Note:</strong> This article only demonstrates the Android (Kotlin) implementation. The same concept applies to iOS using Swift or Objective-C.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b02d87dc6be0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>