我想从本地Android检索String列表,以通过Method Channel颤动。此字符串列表是所有联系电话号码。我当前的代码:
new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler( new MethodChannel.MethodCallHandler() { @Override public void onMethodCall(MethodCall call, MethodChannel.Result result) { if (call.method.equals("getContacts")) { contacts = getContactList(); if (contacts != null) { result.success(contacts); } else { result.error("UNAVAILABLE", "not avilable", null); } } else { result.notImplemented(); } } });
在Flutter中:
final Iterable result = await platform.invokeMethod('getContacts'); contactNumber = result.toList();
但是我没有得到任何回应。如何仅将电话号码从本机android检索到flutter?
这是我的方法。
Android本机代码(带有字符串的发送列表):
new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler( new MethodCallHandler() { @Override public void onMethodCall(MethodCall call, Result result) { if (call.method.equals("samples.flutter.io/contact")) { final List<String> list = new ArrayList<>(); list.add("Phone number 1"); list.add("Phone number 2"); list.add("Phone number 3"); result.success(list); } else { result.notImplemented(); } } } );
颤振代码:
List<dynamic> phoneNumbersList = <dynamic>[]; Future<List<String>> _getList() async { phoneNumbersList = await methodChannel.invokeMethod('samples.flutter.io/contact'); print(phoneNumberList[0]); return phoneNumberList; }