代码之家  ›  专栏  ›  技术社区  ›  Yeahia2508

如何从方法通道中检索字符串列表

  •  1
  • Yeahia2508  · 技术社区  · 6 年前

     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检索到flutter的电话号码?

    2 回复  |  直到 6 年前
        1
  •  5
  •   Gregga17    6 年前

    我是这样做的。

    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;
    }
    
        2
  •  0
  •   Yeahia2508    6 年前

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

    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(",");