【问题标题】:How to retrive List of String from method channel如何从方法通道中检索字符串列表
【发布时间】:2019-02-26 14:07:54
【问题描述】:

我想从原生 Android 检索字符串列表以通过方法通道颤动。这个字符串列表是所有联系电话号码。我当前的代码:

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

在颤振中:

final Iterable result = await platform.invokeMethod('getContacts');
  contactNumber = result.toList();

但我没有从颤振中得到任何回应。如何仅从原生 android 检索电话号码以颤振?

【问题讨论】:

    标签: android flutter


    【解决方案1】:

    这是我的做法。

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

    【讨论】:

    • 如果我有一个自定义类怎么办?我想作为列表检索?
    • @Yeahia2508 如果您有自定义的联系人类别,我认为您唯一需要更改的就是在两个平台上将 List 更改为 List
    • @Gregga17 如何将字符串列表从颤振发送到原生
    • @kartheekij 我没试过,但你可以试试, List list;等待方法channel.invokeMethod('channelName', {"sendListToNativeName": list});在本机端 ArrayList value = call.argument("sendListToNativeName");
    • 对于将 List 更改为 List 的自定义类不起作用,它会抛出 java.lang.IllegalArgumentException: Unsupported value: Contacts。我在下面的自定义类链接上找到了解决方案api.flutter.dev/flutter/services/MethodChannel/…
    【解决方案2】:

    我用另一种简单的方法解决了我的问题。也许对其他人有帮助。

    本机代码:

    package com.y34h1a.test;
    import android.database.Cursor;
    import android.os.Bundle;
    import io.flutter.app.FlutterActivity;
    import io.flutter.plugin.common.MethodCall;
    import io.flutter.plugin.common.MethodChannel;
    import io.flutter.plugins.GeneratedPluginRegistrant;
    import android.provider.ContactsContract;
    
    public class MainActivity extends FlutterActivity {
    
      private static final String CHANNEL = "samples.flutter.io/contact";
      String phoneNumbers = "";
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GeneratedPluginRegistrant.registerWith(this);
    
          new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
                  new MethodChannel.MethodCallHandler() {
                      @Override
                      public void onMethodCall(MethodCall call, MethodChannel.Result result) {
                          if (call.method.equals("getContacts")) {
                              phoneNumbers = getPhoneNumbers();
    
                              if (phoneNumbers != null) {
                                  result.success(phoneNumbers);
                              } else {
                                  result.error("UNAVAILABLE", "Contacts not found", null);
                              }
                          } else {
                              result.notImplemented();
                          }
                      }
                  });
      }
    
      String getPhoneNumbers(){
    
          Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
          while (phones.moveToNext())
          {
              String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    
              if (phoneNumber != null)
                phoneNumbers = phoneNumbers + phoneNumber + ",";
    
          }
          return phoneNumbers;
      }
    }
    

    颤振代码:

    static const platform = const MethodChannel('samples.flutter.io/contact');
    
    final String result = await platform.invokeMethod('getContacts');
    List<String> phoneNumbers = result.split(",");
    

    【讨论】:

      【解决方案3】:

      空安全码

      Java 端(发送数据):

      @Override
      public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine)
      {
          super.configureFlutterEngine(flutterEngine);
      
          new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), "foo_channel")
                  .setMethodCallHandler((methodCall, result) -> {
                      if (methodCall.method.equals("methodInJava")) {
                          // Return your List here.
                          List<String> list = new ArrayList<String>();
                          list.add("A");
                          list.add("B");
                          list.add("C");
                          result.success(list);
                      }
                  });
      }
      

      Dart 端(接收数据):

      void getList() async {
        var channel = MethodChannel('foo_channel');
        List<String>? list = await channel.invokeListMethod<String>('methodInJava');
        print(list); // [A, B, C]
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-15
        • 1970-01-01
        相关资源
        最近更新 更多