繁体   English   中英

错误:参数类型 'MapEntry<string, dynamic> ' 不能分配给参数类型 'Map<string, dynamic> ' Flutter</string,></string,>

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

嘿嘿我是 StackOverflow 的新手:我正在构建一个应用程序,但遇到以下问题:

我在将 Map 转换为 Flutter 中的列表时遇到问题:

我的function(把model有数据转成List):

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();

我的model:

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;
  }
}

这是运行代码时出现的错误:

错误:无法将参数类型“MapEntry<String, dynamic>”分配给参数类型“Map<String, dynamic>”。

看起来您正在尝试获取FinancialsLocalModel的所有LocalCostCenter字段并列出它们。 尝试将您的 function 更改为:

insertFinance(FinancialsModel data) {
  final dataModel = financials_local.FinancialsLocalModel(
    ...
  );

  final dataModelMap = dataModel.toJson();

  final dataModelMapCopy = Map<String, dynamic>.from(dataModelMap); // Copy of dataModelMap
  dataModelMapCopy.removeWhere((key, value) => value is! Map); // Only including the records with the type of LocalCostCenter

  List<financials_local.LocalCostCenter> list = dataModelMapCopy.values.map((e) => financials_local.LocalCostCenter.fromJson(e)).toList(); // A list of all LocalCostCenter fields of dataModel
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM