【问题标题】:Send Map/JSON from Flutter to Android through Method channel通过 Method 通道将 Map/JSON 从 Flutter 发送到 Android
【发布时间】:2020-03-13 11:03:11
【问题描述】:

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

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

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

如何将这些数据从 dart 传递到 kotlin?

【问题讨论】:

    标签: android kotlin flutter dart flutter-platform-channel


    【解决方案1】:

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

    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.
    }
    

    【讨论】:

    • 你也可以在android端使用String userDob = call.argument("user_dob");,为方便起见。
    • @RichardHeap 嘿,感谢您的提示!我实际上也在寻找类似的东西!干杯。
    【解决方案2】:

    映射 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")
    }
    

    【讨论】:

      【解决方案3】:

      飞镖端(发送地图):

      var channel = MethodChannel('foo_channel');
      var map = <String, dynamic>{
        'key': 'value',
      };
      await channel.invokeListMethod<String>('methodInJava', map);
      

      Java端(接收Map):

      if (methodCall.method.equals("methodInJava")) {
          // Map value.
          HashMap<String, Object> map = (HashMap<String, Object>) methodCall.arguments;
          Log.i("MyTag", "map = " + map); // {key=value}
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-25
        • 2013-02-05
        • 1970-01-01
        • 2018-09-29
        • 2021-05-02
        • 1970-01-01
        • 2023-02-06
        相关资源
        最近更新 更多