flutter - Dart列表未通过forEach循环更新

标签 flutter dart

我正在使用此package来检索设备的联系人。库检索427个联系人,我想循环整个列表,以便创建另一个列表并将其发送到后端。问题是循环无法正常工作,此功能在循环完成之前返回。
这是我使用的功能:

  Future<QueryResult> uploadContacts() async {
    final List<Contact> rawContacts =
        (await ContactsService.getContacts(withThumbnails: false)).toList();
    List<ContactInput> contactsListInput;

    print('contactsListInput length: ${rawContacts.length}');
    
    rawContacts.forEach((contact) {
      print('contact: $contact'); //PRINTED JUST ONCE

      //Contact can have more than 1 number. We need them all
      contact.phones.forEach((phone) {
        final contactInput =
            ContactInput(name: contact.displayName, phone: phone.value);

        contactsListInput.add(contactInput);
      });
    });

    print('contactsListInput length: ${contactsListInput.length}'); //NEVER PRINT ANYTHING

    final ContactsListInput input =
        ContactsListInput(contacts: contactsListInput);

    final MutationOptions _options = MutationOptions(
        document: SyncContactsMutation().document,
        variables: SyncContactsArguments(input: input).toJson());

    return client.mutate(_options);
  }
我也尝试过使用for循环,并且发生了同样的事情。
    for (int i = 0; i < rawContacts.length; i++) {
      final contact = rawContacts[i];

      final contactInput =
      ContactInput(name: contact.displayName, phone: contact.phones.first.value);

      contactsListInput.add(contactInput);
    }
    print('contactsListInput length: ${contactsListInput.length}'); //NEVER CALLED

我也尝试过Future.forEach
    await Future.forEach(rawContacts, (contact) async {
      print('contact: $contact');

      //Since contact can have more than one number we loop them too.
      await Future.forEach(contact.phones, (phone) async {
        final contactInput =
            ContactInput(name: contact.displayName, phone: phone.value);

        contactsListInput.add(contactInput);
      });
    });
如何解决这个问题?任何帮助都感激不尽。

最佳答案

我已将其固定为

  Future<QueryResult> uploadContacts() async {
    final Iterable<Contact> rawContacts =
        (await ContactsService.getContacts(withThumbnails: false));

    final Iterable<ContactInput> contacts = rawContacts.expand((contact) => contact.phones.map(
        (phone) =>
            ContactInput(name: contact.displayName, phone: phone.value)));

    final input = ContactsListInput(contacts: contacts);

    final MutationOptions _options = MutationOptions(
        document: SyncContactsMutation().document,
        variables: SyncContactsArguments(input: input).toJson());

    return client.mutate(_options);
  }
归功于@pskink和@ loganrussell48

关于flutter - Dart列表未通过forEach循环更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62632369/

相关文章:

json - Flutter 使用来自 Golang RFC3339 的 DateTime 解析 json : FormatException: Invalid date format

dart - 如何在 Dart 中创建一个空的类型列表

flutter - 文字溢出动画

flutter - 更喜欢 const 文字作为 @immutable 类上构造函数的参数

flutter - flutter 中是否有数字输入字段,该字段附有递增/递减按钮?

dictionary - 如何在抖动中操纵 map ?

unit-testing - `TypeMatcher` 与 `throwsA` 一起使用时不起作用

flutter - 有没有办法禁用 PageView 裁剪效果?

flutter - 在Statelesswidget中为启动屏幕调用方法吗?

canvas - 没有中心点的 Flutter drawArc