Sow*_*ber 3 android dart kotlin flutter flutter-platform-channel
我已经在 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 端。我的数据如下所示:
{
"user_dob":"15 November 1997",
"user_description":"Hello there, I am a User!",
"user_gender":"Male",
"user_session":"KKDSH3G9OJKJFIHXLJXUGWIOG9UJKLJ98WHIJ"
}
Run Code Online (Sandbox Code Playgroud)
如何将这些数据从 dart 传递到 kotlin?
您可以通过方法调用传递参数。喜欢,
var data = {
"user_dob":"15 November 1997",
"user_description":"Hello there, I am a User!",
"user_gender":"Male",
"user_session":"KKDSH3G9OJKJFIHXLJXUGWIOG9UJKLJ98WHIJ"
}
Future<void> _updateProfile() async {
try {
var result = await platform.invokeMethod('updateProfile', data);
print(result);
} on PlatformException catch (e) {
print('Failed : ${e.message}');
}
}
Run Code Online (Sandbox Code Playgroud)
并使用 kotlin 获得结果 call.arguments
MethodChannel(flutterView, channel).setMethodCallHandler { call, result ->
var argData = call.arguments //you can get data in this object.
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2987 次 |
| 最近记录: |