Home > OS >  call java code in the flutter ERROR"MissingPluginException(No implementation found for method P
call java code in the flutter ERROR"MissingPluginException(No implementation found for method P

Time:09-24

how to call java code in the flutter


Doctor summary (to see all details, run flutter doctor -v):

[√] Flutter (Channel stable, 2.5.0, on Microsoft Windows [Version 10.0.19042.1237], locale en-US)

[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)

[√] Chrome - develop for the web

[√] Android Studio (version 2020.3)

[√] IntelliJ IDEA Ultimate Edition (version 2020.3)

[√] VS Code (version 1.60.1)

[√] Connected device (3 available)

• No issues found!


Flutter Code

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  static const platform = MethodChannel("com.flutter.epic/epic");


  @override
  Widget build(BuildContext context) {
    return Center(
      child: ElevatedButton(
        child: Text('Get Battery Level'),
        onPressed: () {
          Printy();
        },
      ),
    );
  }

  void Printy() async {
    String value = "fail";

    try {
      value = await platform.invokeMethod("Printy");
    } catch(e) {
      print(e);
    }

    print(value);
  }
}

Java Code

public class MainActivity extends FlutterActivity {
    private static final String CHANNEL = "com.flutter.epic/epic";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GeneratedPluginRegistrant.registerWith(new FlutterEngine(this));

        new MethodChannel(getFlutterEngine().getDartExecutor().getBinaryMessenger(),CHANNEL).setMethodCallHandler(new MethodChannel.MethodCallHandler() {
            @Override
            public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
                if (call.method.equals("Printy")) {
                    result.success("success!!!");
                }
            }
        });
    }
}

when I click button return this:


I/flutter (12941): MissingPluginException(No implementation found for method Printy on channel com.flutter.epic/epic)

I/flutter (12941): fail


I also try this "https://flutter.dev/docs/development/platform-integration/platform-channels" step by step , but it dose not work in my computer.

how to call java code in the flutter

please , help

CodePudding user response:

The syntax aren't wrong anywhere. Kotlin is the default language unless you explicitly put as Java. I guess you didn't set Java for yours while creating the app. If that's the case then you can convert your project to Java by following Sheruan Bashar's answer.

  • Related