我正在尝试从到PlatformChannels进行通信。实际上尝试按照 flutter 平台频道的文档中的说明进行操作,但方向相反:KotlinFlutter
这个想法是从 MainActivity.kt 类上的 configureFlutterEngine 函数调用 Flutter 函数。
为此,我在 Flutter 方面执行了 main.dart(Flutter 的默认示例):
class _MyHomePageState extends State<MyHomePage> {
static const platformChannel = const MethodChannel('myTestChannel');
@override
Widget build(BuildContext context) {
platformChannel.setMethodCallHandler((call){
print("Hello from ${call.method}");
return null;
});
//
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
//
//
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
],
),
),
);
}
} …Run Code Online (Sandbox Code Playgroud) 我创建了一个新插件
flutter create --template plugin alfalfa
Run Code Online (Sandbox Code Playgroud)
其生成lib/alfalfa.dart包含
import 'dart:async';
import 'package:flutter/services.dart';
class Alfalfa {
static const MethodChannel _channel =
const MethodChannel('alfalfa');
//...
}
Run Code Online (Sandbox Code Playgroud)
我想添加一个EventChannel这样Java和Objective-C代码可以回调到Dart代码。我不知道该叫什么名字EventChannel。
final EventChannel _eventChannel =
const EventChannel("com.rollingfields.alfalfa/events");
Run Code Online (Sandbox Code Playgroud)
或者
final EventChannel _eventChannel =
const EventChannel("alfalfa/events");
Run Code Online (Sandbox Code Playgroud)
或者是其他东西?有约定吗?
如果更好的选择EventChannel是包含反向域的名称,我应该将生成的名称重命名MethodChannel为吗com.rollingfields.alfalfa?
根据flutter platfrom 频道的当前文档,似乎只能从客户端(Dart)调用主机(本机),然后我们从主机获得响应。它的反向是否有任何可能性,例如直接从主机调用客户端?
我似乎不知道如何对通过平台通道从 Flutter 应用程序调用的本机 Android 模块进行调试。我正在使用 Flutter 文档中的示例(https://flutter.dev/docs/development/platform-integration/platform-channels)。我尝试过以下方法:
我究竟做错了什么?
我已经在 Dart 和 Kotlin 中建立了一个基本的方法通道
飞镖代码
Future<void> _updateProfile() async {
try {
var result = await platform.invokeMethod('updateProfile');
print(result);
} on PlatformException catch (e) {
print('Failed to get battery level: ${e.message}');
}
setState(() {
// print('Setting state');
});
}
Run Code Online (Sandbox Code Playgroud)
科特林代码
MethodChannel(flutterView, channel).setMethodCallHandler { call, result ->
if(call.method == "updateProfile"){
val actualResult = updateProfile()
if (actualResult.equals("Method channel Success")) {
result.success(actualResult)
} else {
result.error("UNAVAILABLE", "Result was not what I expected", null)
}
} else {
result.notImplemented()
}
}
Run Code Online (Sandbox Code Playgroud)
我想将 JSON/Map 数据传递给 Kotlin …
我正在 Flutter 中使用 PlatformChannel 和 MethodChannel。做一些基本的操作......正在为颤动进行 opencv 移植......但我不知道如何解决这个问题!
调用简单方法没有问题,返回基本类型的对象,如下所示:
安卓:
@Override
public void onMethodCall(final MethodCall call, final Result result) {
switch (call.method) {
case "getVersionString":
result.success(org.opencv.core.Core.getVersionString());
break;
default:
result.notImplemented();
}
}
Run Code Online (Sandbox Code Playgroud)
镖:
Future<String> get versionString async {
return await _channel.invokeMethod('getVersionString') as String;
}
Run Code Online (Sandbox Code Playgroud)
但是如果我有一些自定义对象呢?类似的东西:
Android:
@Override
public void onMethodCall(final MethodCall call, final Result result) {
switch (call.method) {
case "getVersionString":
result.success(org.opencv.core.Core.getVersionString());
break;
case "Scalar":
result.success(org.opencv.core.Core.Scalar());
break;
default:
result.notImplemented();
}
}
}
Run Code Online (Sandbox Code Playgroud)
镖:
class Scalar {
static const MethodChannel _channel = …Run Code Online (Sandbox Code Playgroud) 当您尝试在发布模式下构建 apk 时,许多插件无法正常工作,但这些插件在调试模式下工作正常
有些人建议将 gradle 版本更改为 3.5 ,但有时使用的插件可能与相同的不兼容 file_picker_cross
其他人建议--no-shrink在构建 apk 时使用选项,即flutter build apk --release --no-shrink
这些解决方案都不适合我,我发现这个解决方案隐藏在 github 问题对话中
检查下面的解决方案
dart flutter flutter-dependencies flutter-plugin flutter-platform-channel
由于 Flutter 中没有实现 AdSense 的包,所以我决定创建一个本地插件。这非常简单,小部件是一个IFrameElement. 我确保在创建插件时指定它仅支持 Web,因为IFrameElement需要import 'dart:html',但每当我尝试编译/构建移动版本时,它都会失败,因为它尝试将插件与dart:html. 我怎样才能解决这个问题?
插入:
import 'dart:html' as html show window;
import 'dart:html';
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'adsense_platform_interface.dart';
/// A web implementation of the AdsensePlatform of the Adsense plugin.
class AdsenseWeb extends AdsensePlatform {
/// Constructs a AdsenseWeb
AdsenseWeb();
static void registerWith(Registrar registrar) {
AdsensePlatform.instance = AdsenseWeb();
}
Widget adsenseAdsView(double width, double height) {
ui.platformViewRegistry.registerViewFactory(
'adViewBlock',
(int viewID) => IFrameElement()
..width = …Run Code Online (Sandbox Code Playgroud) dart-html flutter flutter-plugin flutter-platform-channel flutter-html