Monday, 11 January 2021

No implementation method found for method error

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.



from No implementation method found for method error

No comments:

Post a Comment