如何将数据列表从Flutter发送到Android

时间:2018-12-17 12:06:19

标签: android dart flutter

我致力于如何将字符串从flutter发送到android。

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

有人可以建议一种发送列表的方法吗?

代码:

颤动:

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

Android:

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"},

    ];

2 个答案:

答案 0 :(得分:0)

在此示例-我将地理坐标作为元素列表发送

在Flutter中:

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

在Android中:

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

答案 1 :(得分:0)

您可以做这样的事情

颤振部分

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

在android部分

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