【问题标题】:Dart List doesnt get updated with forEach loopDart List 不会使用 forEach 循环进行更新
【发布时间】:2020-06-29 06:25:39
【问题描述】:

我正在使用这个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);
      });
    });

如何解决这个问题?任何帮助将不胜感激。

【问题讨论】:

  • List&lt;ContactInput&gt; contactsListInput;null - 当 contactsListInput 为空时,您不能调用 contactsListInput.add(contactInput); - 您必须初始化该列表 - 顺便说一句,您不需要那些 forEach / add 等 -只需使用Iterable.map() / Itarable.expand() 方法
  • 在我看来,您的 contactsListInput 列表从未初始化为值。您可能会遇到异常
  • 就这么简单:rawContacts.expand((contact) =&gt; contact.phones.map((phone) =&gt; ContactInput(name: contact.displayName, phone: phone)))
  • 正如我所说:您根本不需要该列表(以及两个 forEach 循环):只需使用 expand() / map() 方法
  • 很好,然后发布一个自我回答

标签: flutter dart


【解决方案1】:

我已将其修复为

  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

【讨论】:

  • 顺便说一句,如果 ContactsListInput 接受 contacts 作为 Iterable 那么你不需要 .asList() - 当然如果 ContactsListInput.contactsList 你必须添加 asList()
  • 现在我需要通过在地图功能中调用await formatPhone(phone.value); 来格式化电话号码。此方法在电话有效时返回字符串,否则返回 null。问题是它返回 Future。我无法在地图内调用。任何建议
  • 似乎你需要Future.wait(或Future.forEach,但恕我直言wait在这种情况下更好)
  • ```await Future.forEach(contacts, (ContactInput contact) async { final phone = await formatPhone(contact.phone); if (phone != null) { final input = ContactInput(name:联系人姓名,电话:电话);contactInputs.add(输入);}});````
  • 我使用 forEach 我只是想知道是否可以在 map/in mao 之后链接一些东西。还是谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-06
  • 2016-05-18
  • 1970-01-01
相关资源
最近更新 更多