[英]How to send data List from flutter to android
I worked on how to send the string from flutter to android. 我致力于如何将字符串从flutter发送到android。
Now I want to send a list from flutter to android. 现在我想将列表从flutter发送到android。
Can anyone suggest a way to send a list? 谁能建议一种发送列表的方法?
code: 码:
flutter: 扑:
const platform = const MethodChannel('app.channel.shared.data');
var sharedData = await platform.invokeMethod("");
Android: 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);
}
}
My list: 我的列表:
My List looks like this. 我的列表如下所示。
List<Map<String, dynamic>> users = [
{ "name": "john" , "age": "17"},
{ "name": "jeoohn" , "age": "57"},
{ "name": "jreohn" , "age": "37"},
];
Here example - I send geo coords as list of elements 这里的例子-我发送地理坐标作为元素列表
In Flutter: 在Flutter中:
await platform.invokeMethod('showLocation', [event.location[0], event.location[1], event.title])
In Android: 在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)
}
you can make something like this 你可以做这样的事情
in flutter part 在颤抖的部分
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));
}
in android part 在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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.