You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dart中List<DTO>转List<Model>遇类型不匹配错误求助

Dart 3.4.1中DTO转业务模型的类型不匹配问题

我使用Dart 3.4.1版本,在将API返回的DTO转换为业务模型时碰到了奇怪的类型不匹配错误。

转换代码如下:

var salesCategoriesDto =
        dataList.map((i) => SalesDailyCategoryResponseDto.fromJson(i)).toList();

var salesCategories = salesCategoriesDto
        .map((i) => SalesDailyCategory.fromDto(salesCategoryDto: i))
        .toList();

错误提示:
error: The argument type 'SalesDailyCategoryResponseDto (定义于D:\workcode\redacted\lib\features\sales\persentation\dtos\sales_daily_category_response_dto.dart)' can't be assigned to the parameter type 'SalesDailyCategoryResponseDto (定义于D:\workcode\redacted\lib\features\sales\persentation\dtos\sales_daily_Category_response_dto.dart)'. (argument_type_not_assignable at [eth_sales_management_system] lib\features\sales\repositories\sales_daily_repository.dart:39)

我已经做了这些尝试,但问题依旧:

  • 修正了非DTO模型中的导入路径
  • IDE的linter可以通过Ctrl+左键跳转到对应文件
  • 去掉私有构造函数后错误仍然存在

相关类代码

SalesDailyCategoryResponseDto.dart

class SalesDailyCategoryResponseDto {
  String? lineGroup;
  int? daySales;
  int? monthSales;
  int? yearSales;
  int? totalSales;

  SalesDailyCategoryResponseDto._({
    required this.lineGroup,
    required this.daySales,
    required this.monthSales,
    required this.yearSales,
    required this.totalSales,
  });

  factory SalesDailyCategoryResponseDto.fromJson(Map<String, dynamic> jsonObject) {
    return SalesDailyCategoryResponseDto._(
      lineGroup: jsonObject["LineGroup"],
      daySales: jsonObject["DaySales"],
      monthSales: jsonObject["MonthSales"],
      yearSales: jsonObject["YearSales"],
      totalSales: jsonObject["TotalSales"],
    );
  }
}

SalesDailyCategory.dart

class SalesDailyCategory {
  String lineGroup;
  int daySales;
  int monthSales;
  int yearSales;
  int totalSales;

  SalesDailyCategory._({
    required this.lineGroup,
    required this.daySales,
    required this.monthSales,
    required this.yearSales,
    required this.totalSales,
  });

  factory SalesDailyCategory.fromDto(
          {required SalesDailyCategoryResponseDto salesCategoryDto}) =>
      SalesDailyCategory._(
        lineGroup: salesCategoryDto.lineGroup!,
        daySales: salesCategoryDto.daySales!,
        monthSales: salesCategoryDto.monthSales!,
        yearSales: salesCategoryDto.yearSales!,
        totalSales: salesCategoryDto.totalSales!,
      );
}

问题根源与解决方法

从错误信息里的两个文件路径能看出核心问题:sales_daily_category_response_dto.dart和sales_daily_Category_response_dto.dart,文件名里的Category首字母一个是小写c,一个是大写C。Dart在大小写敏感的文件系统中会把这两个当成完全不同的文件,导致生成了两个独立的SalesDailyCategoryResponseDto类,自然就出现了类型不匹配。

解决步骤:

  1. 统一文件名大小写:把所有相关文件的文件名改成完全一致的格式,比如统一用小写的sales_daily_category_response_dto.dart
  2. 清理编译缓存:如果是Flutter项目,执行flutter clean;纯Dart项目执行dart pub cache clean,清除旧的编译产物
  3. 重新导入文件:在所有用到该DTO的文件里,重新导入正确的文件,避免IDE缓存的旧路径
  4. 重启IDE:有时候IDE的缓存不会自动更新,重启后能清除残留的错误提示

内容的提问来源于stack exchange,提问作者Enrico Roselino

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 19:32:01