【问题标题】:How to send data List from flutter to android如何将数据列表从颤振发送到android
【发布时间】:2018-12-17 12:06:19
【问题描述】:

我研究了如何将字符串从颤振发送到 android。

现在我想将列表从 Flutter 发送到 android。

谁能推荐一种发送列表的方法?

代码:

颤动:

const platform = const MethodChannel('app.channel.shared.data');
    var sharedData = await platform.invokeMethod("");

安卓:

public class MainActivity extends FlutterActivity {
    Intent in;
    private String sharedText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GeneratedPluginRegistrant.registerWith(this);
        Intent intent = getIntent();
        String action = intent.getAction();
        String type = intent.getType();

        if (Intent.ACTION_SEND.equals(action) && type != null) {
            if ("text/plain".equals(type)) {
                handleSendText(intent); // Handle text being sent
            }
        }

        new MethodChannel(getFlutterView(), "app.channel.shared.data").setMethodCallHandler(
            new MethodCallHandler() {
                @Override
                public void onMethodCall(MethodCall call, MethodChannel.Result result) {
                    if (call.method.contentEquals("getSharedText")) {
                        Log.v("Tag_data",""+call.method);
                        Intent myIntent = new Intent(MainActivity.this, PrinterActviity.class);
                        MainActivity.this.startActivity(myIntent);
                    }
                }
            });
    }

    void handleSendText(Intent intent) {
        sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
    }
}

我的清单:

我的列表如下所示。

List<Map<String, dynamic>> users = [
    { "name": "john" , "age": "17"},
    { "name": "jeoohn" , "age": "57"},
    { "name": "jreohn" , "age": "37"},

    ];

【问题讨论】:

    标签: android dart flutter


    【解决方案1】:

    这里的例子 - 我将地理坐标作为元素列表发送

    在颤振中:

    await platform.invokeMethod('showLocation', [event.location[0], event.location[1], event.title])
    

    在安卓中:

    if (call.method == MAP_METHOD) {
                val args = (call.arguments) as List<Any>
                startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("geo:${args[0]}, ${args[1]}?z=23&q=${args[0]},${args[1]}(${args[2]})")))
                result.success(null)
            }
    

    【讨论】:

      【解决方案2】:

      你可以做这样的事情

      在颤振部分

        startChannle() {
          BasicMessageChannel myChannel =
              BasicMessageChannel<String>('users', StringCodec());
      
          List<Map<String, dynamic>> users = [
            {"name": "john", "age": "17"},
            {"name": "jeoohn", "age": "57"},
            {"name": "jreohn", "age": "37"},
          ];
          myChannel.send(json.encode(users));
        }
      

      在安卓部分

        private fun initMyChannel() {
              myChannel = BasicMessageChannel<String>(flutterView, "users", StringCodec.INSTANCE)
              myChannel.setMessageHandler { message, _ ->
                  Log.i("MSG", "Received: $message")
                  val received = JSONObject(message)
                  // parse json data 
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-05
        • 2021-08-05
        • 2020-07-23
        • 2020-12-11
        • 1970-01-01
        • 2021-10-23
        • 1970-01-01
        相关资源
        最近更新 更多