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

Flutter中Map转List报错:MapEntry无法赋值给Map类型参数

Flutter Map转List时出现类型不匹配错误

我是StackOverflow的新用户,正在开发Flutter应用,遇到了Map转List的问题,运行代码时报错:

Error: The argument type 'MapEntry<String, dynamic>' can't be assigned to the parameter type 'Map<String, dynamic>'.

我的转换函数代码:

insertFinance(FinancialsModel data) {
    final dataModel = financials_local.FinancialsLocalModel(
        period: data.period,
        date: data.date,
        costCenter: financials_local.LocalCostCenter(id: data.costCenter!.id),
        description: data.description,
        quantity: data.quantity,
        value: data.value,
        unitValue: data.unitValue,
        validated: data.validated,
        status: data.status,
        type: data.type,
        harvest: financials_local.LocalCostCenter(id: data.harvest!.id),
        customer: financials_local.LocalCostCenter(id: data.customer!.id),
        farm: financials_local.LocalCostCenter(id: data.farm!.id),
        createdBy: financials_local.LocalCostCenter(id: data.createdBy!.id),
        environment: data.environment,
        sent: false);

    List list = dataModel
        .toJson()
        .entries
        .map<financials_local.LocalCostCenter>((x) => financials_local.LocalCostCenter.fromJson(x))
        .toList();
}

我的模型代码:

class FinancialsLocalModel {
  String? period;
  String? date;
  LocalCostCenter? costCenter;
  String? description;
  int? quantity;
  double? value;
  double? unitValue;
  bool? validated;
  bool? status;
  String? type;
  LocalCostCenter? harvest;
  LocalCostCenter? customer;
  LocalCostCenter? farm;
  LocalCostCenter? createdBy;
  String? environment;
  bool? sent;

  FinancialsLocalModel(
      {this.period,
      this.date,
      this.costCenter,
      this.description,
      this.quantity,
      this.value,
      this.unitValue,
      this.validated,
      this.status,
      this.type,
      this.harvest,
      this.customer,
      this.farm,
      this.createdBy,
      this.environment,
      this.sent});

  FinancialsLocalModel.fromJson(Map<String, dynamic> json) {
    period = json['period'];
    date = json['date'];
    costCenter = json['cost_center'] != null
        ? LocalCostCenter.fromJson(json['cost_center'])
        : null;
    description = json['description'];
    quantity = json['quantity'];
    value = json['value'];
    unitValue = json['unit_value'];
    validated = json['validated'];
    status = json['status'];
    type = json['type'];
    harvest = json['harvest'] != null
        ? LocalCostCenter.fromJson(json['harvest'])
        : null;
    customer = json['customer'] != null
        ? LocalCostCenter.fromJson(json['customer'])
        : null;
    farm = json['farm'] != null ? LocalCostCenter.fromJson(json['farm']) : null;
    createdBy = json['created_by'] != null
        ? LocalCostCenter.fromJson(json['created_by'])
        : null;
    environment = json['environment'];
    sent = (json['sent'] == 0) ? false : true;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['period'] = period;
    data['date'] = date;
    if (costCenter != null) {
      data['cost_center'] = costCenter!.toJson();
    }
    data['description'] = description;
    data['quantity'] = quantity;
    data['value'] = value;
    data['unit_value'] = unitValue;
    data['validated'] = validated;
    data['status'] = status;
    data['type'] = type;
    if (harvest != null) {
      data['harvest'] = harvest!.toJson();
    }
    if (customer != null) {
      data['customer'] = customer!.toJson();
    }
    if (farm != null) {
      data['farm'] = farm!.toJson();
    }
    if (createdBy != null) {
      data['created_by'] = createdBy!.toJson();
    }
    data['environment'] = environment;
    data['sent'] = (!sent!) ? 0 : 1;
    return data;
  }
}

class LocalCostCenter {
  int? id;

  LocalCostCenter({this.id});

  LocalCostCenter.fromJson(Map<String, dynamic> json) {
    id = json['id'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    return data;
  }
}

问题原因

你调用dataModel.toJson().entries得到的是MapEntry<String, dynamic>的迭代器,每个x是键值对,而LocalCostCenter.fromJson()需要的是Map<String, dynamic>类型参数,直接传x就会类型不匹配。

另外从代码逻辑看,你实际想提取FinancialsLocalModel里所有LocalCostCenter类型的字段转成List,而非把整个Map的所有键值对都转成LocalCostCenter——毕竟Map里还有字符串、数字、布尔等非LocalCostCenter类型的字段,强行转换也会出错。

解决方案

方案一:直接提取模型字段(推荐)

手动收集所有LocalCostCenter类型的字段,过滤掉null值后组成List,无需经过json转换,效率更高:

insertFinance(FinancialsModel data) {
    final dataModel = financials_local.FinancialsLocalModel(
        period: data.period,
        date: data.date,
        costCenter: financials_local.LocalCostCenter(id: data.costCenter!.id),
        description: data.description,
        quantity: data.quantity,
        value: data.value,
        unitValue: data.unitValue,
        validated: data.validated,
        status: data.status,
        type: data.type,
        harvest: financials_local.LocalCostCenter(id: data.harvest!.id),
        customer: financials_local.LocalCostCenter(id: data.customer!.id),
        farm: financials_local.LocalCostCenter(id: data.farm!.id),
        createdBy: financials_local.LocalCostCenter(id: data.createdBy!.id),
        environment: data.environment,
        sent: false);

    // 提取所有LocalCostCenter类型字段,过滤null值
    List<financials_local.LocalCostCenter> list = [
      dataModel.costCenter,
      dataModel.harvest,
      dataModel.customer,
      dataModel.farm,
      dataModel.createdBy,
    ].where((item) => item != null).cast<financials_local.LocalCostCenter>().toList();
}

方案二:从json Map中提取

如果必须通过toJson()的Map来处理,可以先筛选出值为Map类型的条目,再将值传入fromJson:

insertFinance(FinancialsModel data) {
    final dataModel = financials_local.FinancialsLocalModel(
        period: data.period,
        date: data.date,
        costCenter: financials_local.LocalCostCenter(id: data.costCenter!.id),
        description: data.description,
        quantity: data.quantity,
        value: data.value,
        unitValue: data.unitValue,
        validated: data.validated,
        status: data.status,
        type: data.type,
        harvest: financials_local.LocalCostCenter(id: data.harvest!.id),
        customer: financials_local.LocalCostCenter(id: data.customer!.id),
        farm: financials_local.LocalCostCenter(id: data.farm!.id),
        createdBy: financials_local.LocalCostCenter(id: data.createdBy!.id),
        environment: data.environment,
        sent: false);

    List<financials_local.LocalCostCenter> list = dataModel
        .toJson()
        .entries
        // 筛选出值为Map类型的条目(对应LocalCostCenter转成的json)
        .where((entry) => entry.value is Map<String, dynamic>)
        .map<financials_local.LocalCostCenter>((entry) => financials_local.LocalCostCenter.fromJson(entry.value as Map<String, dynamic>))
        .toList();
}

内容的提问来源于stack exchange,提问作者Henrique Brito

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 07:35:36