【问题标题】:Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<InvoiceItemDto>?'未处理的异常:类型 \'List<dynamic>\' 不是类型 \'List<InvoiceItemDto>?\' 的子类型
【发布时间】:2023-02-26 00:40:19
【问题描述】:

我有这个列表 JSON 输出

 "invoiceItems": [
          {
            "description": null,
            "id": "13",
          }
        ],

我使用下面的代码将其转换为List&lt;InvoiceItemDto&gt;

 invoiceItems: json['invoiceItems'].map((data) => InvoiceItemDto.fromJson(data)).toList(),

但它抛出我错误

 Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<InvoiceItemDto>?'

invoice_item_dto.dart 文件

class InvoiceItemDto {
  final int? id;
  final String? description;    

  const InvoiceItemDto({
   this.id,
   this.description,
  });

  factory InvoiceItemDto.fromJson(Map<String, dynamic> json) {
    return InvoiceItemDto(
      id: json['id'],
      description: json['address'],
    );
  }

  InvoiceItemEntity toInvoiceItemEntity() {
    return InvoiceItemEntity(
      id: id,description: description);
  }
}

【问题讨论】:

  • 请添加根模型的完整代码
  • @AlexSunderSingh 帖子已编辑。
  • 我需要包含invoiceItems: json['invoiceItems'].map((data) =&gt;InvoiceItemDto.fromJson(data)).toList(),的代码

标签: json flutter


【解决方案1】:

尝试使用List.from

invoiceItems: List.from(json['invoiceItems'].map((data) => InvoiceItemDto.fromJson(data)));

【讨论】:

    猜你喜欢
    • 2021-09-16
    • 2021-04-04
    • 1970-01-01
    • 2022-09-26
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多