如何将消息从 Flutter 传递到 Native?
How to pass a message from Flutter to Native?
如果需要与特定 API / 硬件组件交互,您将如何将信息从 Flutter 传回 Android/Native 代码?
是否有任何事件通道可以以其他方式或类似于回调的方式发送信息?
- platform_channel documentation points out "method calls can also be sent in the reverse direction, with the platform acting as client to methods implemented in Dart. A concrete example of this is the quick_actions plugin。”我没有看到本机端在这种情况下如何从 Flutter 接收消息。
- 看来BasicMessageChannel的send()方法可以用来发送"the specified message to the platform plugins on this channel"。谁能提供一个简单的实现示例?
是的,flutter 确实有一个 EventChannel
class,这正是您要找的。
Here 是演示如何实现 MethodChannel
和 EventChannel
的示例。和
this 中篇文章展示了如何在 flutter 中实现 EventChannel
。
希望对您有所帮助!
这是一个简单的实现展示:
- 将字符串值从 flutter 传递到 Android 代码
- 从 Android 代码返回响应以进行 flutter
代码基于以下示例:https://flutter.io/platform-channels/#codec
1.Passing 字符串值 "text" :
String text = "whatever";
Future<Null> _getBatteryLevel(text) async {
String batteryLevel;
try {
final String result = await platform.invokeMethod('getBatteryLevel',{"text":text});
batteryLevel = 'Battery level at $result % .';
} on PlatformException catch (e) {
batteryLevel = "Failed to get battery level: '${e.message}'.";
}
setState(() {
_batteryLevel = batteryLevel;
});
}
2.Getting 返回响应 "batterylevel" 在 RandomFunction() 之后;
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
if (call.method.equals("getBatteryLevel")) {
text = call.argument("text");
String batteryLevel = RandomFunction(text);
if (batteryLevel != null) {
result.success(batteryLevel);
} else {
result.error("UNAVAILABLE", "Battery level not available.", null);
}
} else {
result.notImplemented();
}
}
希望对您有所帮助!
Objective C
call.arguments[@"parameter"]
Android
call.argument("parameter");
对于swift
guard let args = call.arguments as? [String : Any] else {return}
let phoneNumber = args["contactNumber"] as! String
let originalMessage = args["message"] as! String
如果有人想用invoke方法分享从native到flutter的数据
按照这个:
main.dart
Future<dynamic> handlePlatformChannelMethods() async {
platform.setMethodCallHandler((methodCall) async {
if (methodCall.method == "nativeToFlutter") {
String text = methodCall.arguments;
List<String> result = text.split(' ');
String user = result[0];
String message = result[1];
}
}
}
MainActivity.java
nativeToFlutter(text1:String?,text2:String?){
MethodChannel(flutterEngine!!.dartExecutor.binaryMessenger,
CHANNEL.invokeMethod("nativeToFlutter",text1+" "+text2);
}
如果需要与特定 API / 硬件组件交互,您将如何将信息从 Flutter 传回 Android/Native 代码?
是否有任何事件通道可以以其他方式或类似于回调的方式发送信息?
- platform_channel documentation points out "method calls can also be sent in the reverse direction, with the platform acting as client to methods implemented in Dart. A concrete example of this is the quick_actions plugin。”我没有看到本机端在这种情况下如何从 Flutter 接收消息。
- 看来BasicMessageChannel的send()方法可以用来发送"the specified message to the platform plugins on this channel"。谁能提供一个简单的实现示例?
是的,flutter 确实有一个 EventChannel
class,这正是您要找的。
Here 是演示如何实现 MethodChannel
和 EventChannel
的示例。和
this 中篇文章展示了如何在 flutter 中实现 EventChannel
。
希望对您有所帮助!
这是一个简单的实现展示:
- 将字符串值从 flutter 传递到 Android 代码
- 从 Android 代码返回响应以进行 flutter
代码基于以下示例:https://flutter.io/platform-channels/#codec
1.Passing 字符串值 "text" :
String text = "whatever";
Future<Null> _getBatteryLevel(text) async {
String batteryLevel;
try {
final String result = await platform.invokeMethod('getBatteryLevel',{"text":text});
batteryLevel = 'Battery level at $result % .';
} on PlatformException catch (e) {
batteryLevel = "Failed to get battery level: '${e.message}'.";
}
setState(() {
_batteryLevel = batteryLevel;
});
}
2.Getting 返回响应 "batterylevel" 在 RandomFunction() 之后;
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
if (call.method.equals("getBatteryLevel")) {
text = call.argument("text");
String batteryLevel = RandomFunction(text);
if (batteryLevel != null) {
result.success(batteryLevel);
} else {
result.error("UNAVAILABLE", "Battery level not available.", null);
}
} else {
result.notImplemented();
}
}
希望对您有所帮助!
Objective C
call.arguments[@"parameter"]
Android
call.argument("parameter");
对于swift
guard let args = call.arguments as? [String : Any] else {return}
let phoneNumber = args["contactNumber"] as! String
let originalMessage = args["message"] as! String
如果有人想用invoke方法分享从native到flutter的数据 按照这个:
main.dart
Future<dynamic> handlePlatformChannelMethods() async {
platform.setMethodCallHandler((methodCall) async {
if (methodCall.method == "nativeToFlutter") {
String text = methodCall.arguments;
List<String> result = text.split(' ');
String user = result[0];
String message = result[1];
}
}
}
MainActivity.java
nativeToFlutter(text1:String?,text2:String?){
MethodChannel(flutterEngine!!.dartExecutor.binaryMessenger,
CHANNEL.invokeMethod("nativeToFlutter",text1+" "+text2);
}