首页 > 解决方案 > 升级颤振后创建方法通道 - 无法解析方法 getFlutterView()

问题描述

我在我的颤振应用程序中使用原生 android 方法,使用文档说使用

MethodChannel(flutterView, CHANNEL).setMethodCallHandler...

但升级颤振后,该MethodChannel功能不再需要flutterView,也flutterView不再存在。

can not resolve method getFlutterView()

我认为应该有一个创建频道的新教程

相反,它需要一些BinaryMessenger我不知道该给的东西。

这是不再工作的旧代码:

import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "samples.flutter.dev/battery";

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);

    new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
            new MethodCallHandler() {
                @Override
                public void onMethodCall(MethodCall call, Result result) {
                    // Note: this method is invoked on the main thread.
                    // TODO
                }
            });
}

标签: androidflutterflutter-channel

解决方案


替换getFlutterView()getFlutterEngine().getDartExecutor().getBinaryMessenger()

您实际上并不需要.getBinaryMessenger()asDartExecutor实现BinaryMessenger本身(仅通过转发),但我认为指定信使更正确。


推荐阅读