通过 Method 通道将 Map/JSON 从 Flutter 发送到 Android
Send Map/JSON from Flutter to Android through Method 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');
});
}
Kotlin 代码
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()
}
}
我想向 Kotlin 端传递一个 JSON/Map 数据。我的数据如下所示:
{
"user_dob":"15 November 1997",
"user_description":"Hello there, I am a User!",
"user_gender":"Male",
"user_session":"KKDSH3G9OJKJFIHXLJXUGWIOG9UJKLJ98WHIJ"
}
如何将此数据从 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}');
}
}
并使用 call.arguments
在 kotlin 中获取结果
MethodChannel(flutterView, channel).setMethodCallHandler { call, result ->
var argData = call.arguments //you can get data in this object.
}
地图Json:
{
"user_dob":"15 November 1997",
"user_description":"Hello there, I am a User!",
"user_gender":"Male",
"user_session":"KKDSH3G9OJKJFIHXLJXUGWIOG9UJKLJ98WHIJ"
}
Kotlin代码:
MethodChannel(flutterView, channel).setMethodCallHandler { call, result ->
//you can get data in this object.
val user_dob = call.argument<String>("user_dob")
val user_description = call.argument<String>("user_description")
val user_gender = call.argument<String>("user_gender")
val user_session = call.argument<String>("user_session")
}
镖方(送图):
var channel = MethodChannel('foo_channel');
var map = <String, dynamic>{
'key': 'value',
};
await channel.invokeListMethod<String>('methodInJava', map);
Java方(接收地图):
if (methodCall.method.equals("methodInJava")) {
// Map value.
HashMap<String, Object> map = (HashMap<String, Object>) methodCall.arguments;
Log.i("MyTag", "map = " + map); // {key=value}
}
我已经在 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');
});
}
Kotlin 代码
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()
}
}
我想向 Kotlin 端传递一个 JSON/Map 数据。我的数据如下所示:
{
"user_dob":"15 November 1997",
"user_description":"Hello there, I am a User!",
"user_gender":"Male",
"user_session":"KKDSH3G9OJKJFIHXLJXUGWIOG9UJKLJ98WHIJ"
}
如何将此数据从 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}');
}
}
并使用 call.arguments
MethodChannel(flutterView, channel).setMethodCallHandler { call, result ->
var argData = call.arguments //you can get data in this object.
}
地图Json:
{
"user_dob":"15 November 1997",
"user_description":"Hello there, I am a User!",
"user_gender":"Male",
"user_session":"KKDSH3G9OJKJFIHXLJXUGWIOG9UJKLJ98WHIJ"
}
Kotlin代码:
MethodChannel(flutterView, channel).setMethodCallHandler { call, result ->
//you can get data in this object.
val user_dob = call.argument<String>("user_dob")
val user_description = call.argument<String>("user_description")
val user_gender = call.argument<String>("user_gender")
val user_session = call.argument<String>("user_session")
}
镖方(送图):
var channel = MethodChannel('foo_channel');
var map = <String, dynamic>{
'key': 'value',
};
await channel.invokeListMethod<String>('methodInJava', map);
Java方(接收地图):
if (methodCall.method.equals("methodInJava")) {
// Map value.
HashMap<String, Object> map = (HashMap<String, Object>) methodCall.arguments;
Log.i("MyTag", "map = " + map); // {key=value}
}