No implementation method found for method error

594

Solution 1

When you use your flutter project as a library module in another project, and navigate to the Flutter part using FlutterActivity.createDefaultIntent(getApplicationContext()) , your MainActivity will not be evaluated. I suggest that you create a new Activity in your native app (LoginApp in your case) and have it extending FlutterActivity, and start it when you want to navigate to the flutter part (see below)

// in LoginApp 

import androidx.annotation.NonNull;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;

class MyFlutterActivity extends FlutterActivity {
    private static final String CHANNEL = "samples.flutter.io/battery";

    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);

        new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL).setMethodCallHandler(((methodCall, result) -> {
            if (methodCall.method.equals("getBatteryLevel")) {

                result.success("batteryLevel"); // It returns string "batteryLevel".

            } else {
                result.notImplemented();
            }
        }));
    }
}

and then start it like this

    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(
                    new FlutterActivity.NewEngineIntentBuilder(MyFlutterActivity.class).build(getApplicationContext())
            );
        }
    });

And don't forget to list this activity in your android manifest (in LoginApp)

    <activity android:name=".ui.login.MyFlutterActivity"
        android:theme="@style/AppTheme"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
        android:hardwareAccelerated="true"
        android:windowSoftInputMode="adjustResize"
        />

Solution 2

Please specify the method channel in your dart code as exact as the CHANNEL string in your native code. For this scenario, you will have to write the below code in your flutter side code

static const methodChannel = const MethodChannel('samples.flutter.io/battery');

 String result = await methodChannel.invokeMethod('getBatteryLevel');
Share:
594
Akif
Author by

Akif

I'm developing web and mobile apps. Contact me via: https://www.linkedin.com/in/deremakif/ or https://superpeer.com/deremakif

Updated on December 26, 2022

Comments

  • Akif
    Akif over 1 year

    I have a simple Android app. I imported a new flutter module. When I pass to the Flutter page, I can not implement native code anymore. I got an error No implementation method found for method getBatteryLevel. How can I fix this?

    Note: If I run the flutter module itself, I can get the result of native code.

    The triggered method in main.dart:

     String result = await methodChannel.invokeMethod('getBatteryLevel');
    

    MainActivity of Flutter module:

    
    package com.deremakif.flutter_counter_module.host;
    
    import io.flutter.embedding.android.FlutterActivity;
    
    import androidx.annotation.NonNull;
    import android.content.ContextWrapper;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.os.BatteryManager;
    import android.os.Build.VERSION;
    import android.os.Build.VERSION_CODES;
    import android.os.Bundle;
    import io.flutter.embedding.engine.FlutterEngine;
    import io.flutter.plugin.common.MethodCall;
    import io.flutter.plugin.common.MethodChannel;
    import io.flutter.plugins.GeneratedPluginRegistrant;
    
    public class MainActivity extends FlutterActivity {
    
        private static final String CHANNEL =  "samples.flutter.io/battery";
       
        @Override
        public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
          GeneratedPluginRegistrant.registerWith(flutterEngine);
    
            new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL).setMethodCallHandler(((methodCall, result) -> {
              if (methodCall.method.equals("getBatteryLevel")) {
              
                  result.success("batteryLevel"); // It returns string "batteryLevel".
      
              } else {
                result.notImplemented();
              }    
            }));
        }
    }
    
    

    Login button to change the page from Android to Flutter:

    loginButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(
                            FlutterActivity.createDefaultIntent(getApplicationContext())
                    );
                }
            });
    

    You can find the whole project to reproduce the error here.

    • Gazihan Alankus
      Gazihan Alankus over 3 years
      You can set breakpoints in the Java code and start the app in Android Studio (or attach to the process later) to see whether configureFlutterEngine is actually called.
  • Akif
    Akif over 3 years
    Thank you for your response. But the issue is more than defining the channel and method names. I can not access native code when imported a Flutter module into an Android App. This is the issue. .android/ folder is a generated folder.
  • Gourango Sutradhar
    Gourango Sutradhar over 3 years
    In this case can you please look into the folder that you have two classes with same name? As you have the flutter activity with the name MainActivity also if you have the same-named activity for your Native Activity class then it may create complexity. So change the Class name to something rather than MainActivity
  • Akif
    Akif over 3 years
    No, I don't have two classes with the same name. You can look into my GitHub repo.
  • Akif
    Akif over 3 years
    Thank you so much!
  • Gourango Sutradhar
    Gourango Sutradhar over 3 years
    Please provide the git repo so that I can look into the problems
  • Akif
    Akif over 3 years
    I was mentioned about the repo in my question. But @Muhannad Fakhouri has already solved my problem. Thank you for your interest!
  • Akif
    Akif over 3 years
    Hey, I have a new question here: stackoverflow.com/questions/65780859/…
  • Elvis Teles
    Elvis Teles over 2 years
    Thank you so much! That was exactly my problem.