[通过Map / JSON通过方法通道将Map / JSON发送到Android

问题描述 投票:0回答:1

我已经在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');
    });
  }

科特林码

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()
      }
    }

我想将JSON / Map数据传递到Kotlin一侧。我的数据如下所示:

{    
    "user_dob":"15 November 1997", 
    "user_description":"Hello there, I am a User!", 
    "user_gender":"Male", 
    "user_session":"KKDSH3G9OJKJFIHXLJXUGWIOG9UJKLJ98WHIJ"
}

如何将这些数据从飞镖传递到Kotlin?

android kotlin flutter dart flutter-platform-channel
1个回答
0
投票

您可以通过方法调用传递参数。喜欢,

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.
}
© www.soinside.com 2019 - 2024. All rights reserved.