【问题标题】:Iterable toList() throws - ArgumentError (Invalid argument(s) (input): Must not be null)可迭代 toList() 抛出 - ArgumentError(无效参数(输入):不能为空)
【发布时间】:2020-11-09 05:59:31
【问题描述】:

我正在尝试从我的可迭代对象中创建一个列表,但它会引发以下错误

发生了异常。 ArgumentError(无效参数(输入): 不能为空)

关于toList()的

List<ContactModel> iterable = (await pluginContactService.ContactsService.getContacts(withThumbnails: false))
    .where((contact) => contact.phones.firstWhere((item) => item.label == 'mobile', orElse: () => null)?.value != null)
    .map((contact) => ContactModel(
        contact.displayName, contact.phones.firstWhere((item) => item.label == 'mobile').value)).toList(growable: false);

它一遍又一遍地重复这个异常。如果我关闭异常,我会返回一个包含预期结果的列表。我该怎么办?

【问题讨论】:

  • 要显示代码时最好使用代码而不是图像。
  • 更新了@JerryZhou的问题
  • 行号,字符号?指出显示问题的输出到底在哪里
  • 异常出现在 .toList(growable: false);最后

标签: list flutter dart iterable


【解决方案1】:

在调试会话设置中关闭“Break on All Exception”可以解决此问题。

【讨论】:

    【解决方案2】:

    此错误:ArgumentError(Invalid argument(s) input: must not be null) 表示 .toList(growable: false); 调用 null

    因此,您应该在调用.toList() 之前检查它是否为空,或者使用?? [] 之类的默认值来修复此错误。

    ?? [] 可以修复。

    final tmp = (await pluginContactService
        .ContactsService
        .getContacts(withThumbnails: false)
    )
        .where(
           (contact) => contact.phones.firstWhere(
            (item) => item.label == 'mobile', 
            orElse: () => null
           )?.value != null
        )
        .map((contact) => ContactModel(
            contact.displayName, 
            contact.phones.firstWhere(
               (item) => item.label == 'mobile'
            ).value)
         );
    List<ContactModel> iterable = tmp == null? [] : tmp.toList(growable: false);
    
    

    【讨论】:

    • 它不能解决问题。你能解释一下你想要做什么吗?
    • 它仍然会产生异常。
    • @MikeOttink 请尝试新的。
    猜你喜欢
    • 2021-07-21
    • 2020-09-07
    • 2021-06-24
    • 2021-10-26
    • 2021-04-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多