升级 flutter 后创建方法通道 - 无法解析方法 getFlutterView()
create method channel after upgrading flutter- can not resolve method getFlutterView()
我在我的 flutter 应用程序中使用本机 android 方法,使用的文档说 use
MethodChannel(flutterView, CHANNEL).setMethodCallHandler...
但升级flutter后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
}
});
}
将 getFlutterView()
替换为 getFlutterEngine().getDartExecutor().getBinaryMessenger()
。
您实际上不需要 .getBinaryMessenger()
,因为 DartExecutor
实现了 BinaryMessenger
本身(仅通过转发),但我认为指定信使更正确。
我花了好几天时间想弄清楚如何将 Flutter UI 添加到我现有的 Android 应用程序中。最大的挑战是让 MethodChannel 与从 MainActivity 调用的 FlutterActivity 一起工作。我知道这与此处提出的问题略有不同,但是当我搜索 'Android FlutterActivity MethodChannel' 时返回了 post。在浏览了很多关于如何做到这一点的资源之后,我终于在这里找到了我的解决方案:
https://github.com/flutter/samples/tree/master/add_to_app/android_using_plugin/app/src/main/java/dev/flutter/example/androidusingplugin
最初,在 Android Studio 中,打开现有的应用程序,依次点击文件、新建、新建模块、Flutter 模块。我收到错误消息,必须执行手动步骤。
我的 objective 是在 MainActivity - onCreate 中启动 FlutterActivity(在 flutter_module 中打开 main.dart),然后利用尽可能多的原生 Flutter 代码开发 Flutter 'screens'可能,使用 MethodChannel 进行有限的平台调用。在我开发替换 Flutter 代码时,我将继续注释掉现有的 Android 代码。
以下是最终对我有用的方法:
../App_Project/Android/Existing_Android_App/settings.gradle
include ':app'
setBinding(new Binding([gradle: this]))
evaluate(new File(settingsDir.parentFile, '../flutter_module/.android/include_flutter.groovy'))
include ':flutter_module’
project(':flutter_module’).projectDir = new File('../../flutter_module’)
rootProject.name=‘existing_android_app’
../App_Project/Android/Existing_Android_App/app/build.gradle
dependencies {
…
implementation project(':flutter')
}
../App_Project/Android/Existing_Android_App/app/src/main/AndroidManifest.xml
<activity
android:name="io.flutter.embedding.android.FlutterActivity"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize" />
../App_Project/Android/Existing_Android_App/app/src/main/java/com/existing_android_app/MainActivity.java
package com.existing_android_app;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterEngineCache;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
public class MainActivity extends AppCompatActivity {
final String ENGINE_ID = "1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FlutterEngine flutterEngine = new FlutterEngine(this);
flutterEngine.getDartExecutor().executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault());
FlutterEngineCache.getInstance().put(ENGINE_ID, flutterEngine);
MethodChannel channel = new MethodChannel(flutterEngine.getDartExecutor(), "com.existing_android_app/myMethodChannel");
channel.setMethodCallHandler(
new MethodChannel.MethodCallHandler() {
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
String url = call.argument("url");
if (call.method.equals("openBrowser")) {
openBrowser(url);
}
else {
result.notImplemented();
}
}
});
startActivity(FlutterActivity.withCachedEngine(ENGINE_ID).build(this));
}
void openBrowser(String url) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
this.startActivity(intent);
}
}
../App_Project/flutter_module/lib/home_page.飞镖
class AppHomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<AppHomePage> {
static const platform = const MethodChannel(‘com.existing_android_app/myMethodChannel’);
Future<void> _openBrowser() async {
try {
final int result = await platform.invokeMethod('openBrowser', <String, String> { 'url': "http://bing.com” });
}
catch (e) {
print('***** _openBrowser error: ' + e.toString());
}
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: CustomAppBar(),
body: Column(
children: <Widget>[
RaisedButton(
label: Text('Search',
style: TextStyle(fontSize: 18.0),
),
onPressed: () { _openBrowser(); },
) // RaisedButton.icon
], // Widget
) // Column
) // Scaffold
); // SafeArea
}
删除此 import io.flutter.embedding.android.FlutterActivity;
添加这个导入io.flutter.app.FlutterActivity;
为我工作
只需将此方法添加到您的 class:
BinaryMessenger getFlutterView(){
return getFlutterEngine().getDartExecutor().getBinaryMessenger();
}
然后可选择将所有(重构 > 重命名)"getFlutterView" 替换为 "getBinaryMessenger" 以获得更具可读性的代码:
BinaryMessenger getBinaryMessenger(){
return getFlutterEngine().getDartExecutor().getBinaryMessenger();
}
我在我的 flutter 应用程序中使用本机 android 方法,使用的文档说 use
MethodChannel(flutterView, CHANNEL).setMethodCallHandler...
但升级flutter后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
}
});
}
将 getFlutterView()
替换为 getFlutterEngine().getDartExecutor().getBinaryMessenger()
。
您实际上不需要 .getBinaryMessenger()
,因为 DartExecutor
实现了 BinaryMessenger
本身(仅通过转发),但我认为指定信使更正确。
我花了好几天时间想弄清楚如何将 Flutter UI 添加到我现有的 Android 应用程序中。最大的挑战是让 MethodChannel 与从 MainActivity 调用的 FlutterActivity 一起工作。我知道这与此处提出的问题略有不同,但是当我搜索 'Android FlutterActivity MethodChannel' 时返回了 post。在浏览了很多关于如何做到这一点的资源之后,我终于在这里找到了我的解决方案: https://github.com/flutter/samples/tree/master/add_to_app/android_using_plugin/app/src/main/java/dev/flutter/example/androidusingplugin
最初,在 Android Studio 中,打开现有的应用程序,依次点击文件、新建、新建模块、Flutter 模块。我收到错误消息,必须执行手动步骤。
我的 objective 是在 MainActivity - onCreate 中启动 FlutterActivity(在 flutter_module 中打开 main.dart),然后利用尽可能多的原生 Flutter 代码开发 Flutter 'screens'可能,使用 MethodChannel 进行有限的平台调用。在我开发替换 Flutter 代码时,我将继续注释掉现有的 Android 代码。
以下是最终对我有用的方法:
../App_Project/Android/Existing_Android_App/settings.gradle
include ':app'
setBinding(new Binding([gradle: this]))
evaluate(new File(settingsDir.parentFile, '../flutter_module/.android/include_flutter.groovy'))
include ':flutter_module’
project(':flutter_module’).projectDir = new File('../../flutter_module’)
rootProject.name=‘existing_android_app’
../App_Project/Android/Existing_Android_App/app/build.gradle
dependencies {
…
implementation project(':flutter')
}
../App_Project/Android/Existing_Android_App/app/src/main/AndroidManifest.xml
<activity
android:name="io.flutter.embedding.android.FlutterActivity"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize" />
../App_Project/Android/Existing_Android_App/app/src/main/java/com/existing_android_app/MainActivity.java
package com.existing_android_app;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterEngineCache;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
public class MainActivity extends AppCompatActivity {
final String ENGINE_ID = "1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FlutterEngine flutterEngine = new FlutterEngine(this);
flutterEngine.getDartExecutor().executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault());
FlutterEngineCache.getInstance().put(ENGINE_ID, flutterEngine);
MethodChannel channel = new MethodChannel(flutterEngine.getDartExecutor(), "com.existing_android_app/myMethodChannel");
channel.setMethodCallHandler(
new MethodChannel.MethodCallHandler() {
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
String url = call.argument("url");
if (call.method.equals("openBrowser")) {
openBrowser(url);
}
else {
result.notImplemented();
}
}
});
startActivity(FlutterActivity.withCachedEngine(ENGINE_ID).build(this));
}
void openBrowser(String url) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
this.startActivity(intent);
}
}
../App_Project/flutter_module/lib/home_page.飞镖
class AppHomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<AppHomePage> {
static const platform = const MethodChannel(‘com.existing_android_app/myMethodChannel’);
Future<void> _openBrowser() async {
try {
final int result = await platform.invokeMethod('openBrowser', <String, String> { 'url': "http://bing.com” });
}
catch (e) {
print('***** _openBrowser error: ' + e.toString());
}
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: CustomAppBar(),
body: Column(
children: <Widget>[
RaisedButton(
label: Text('Search',
style: TextStyle(fontSize: 18.0),
),
onPressed: () { _openBrowser(); },
) // RaisedButton.icon
], // Widget
) // Column
) // Scaffold
); // SafeArea
}
删除此 import io.flutter.embedding.android.FlutterActivity;
添加这个导入io.flutter.app.FlutterActivity;
为我工作
只需将此方法添加到您的 class:
BinaryMessenger getFlutterView(){
return getFlutterEngine().getDartExecutor().getBinaryMessenger();
}
然后可选择将所有(重构 > 重命名)"getFlutterView" 替换为 "getBinaryMessenger" 以获得更具可读性的代码:
BinaryMessenger getBinaryMessenger(){
return getFlutterEngine().getDartExecutor().getBinaryMessenger();
}