【问题标题】:Error: The argument type 'MapEntry<String, dynamic>' can't be assigned to the parameter type 'Map<String, dynamic>' Flutter错误:参数类型 \'MapEntry<String, dynamic>\' 无法赋值给参数类型 \'Map<String, dynamic>\' Flutter
【发布时间】:2022-11-22 01:51:53
【问题描述】:

你好!我是 Stack Overflow 的新手。我正在构建一个应用程序,但遇到以下问题:

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

我的功能(将带有数据的模型转换为列表):

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

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

错误:参数类型 'MapEntry<String, dynamic>' 不能 分配给参数类型“Map<String, dynamic>”。

【问题讨论】:

  • 您正在将 FinancialsLocalModel 数据转换为什么列表?

标签: flutter dart


【解决方案1】:

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

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
}

【讨论】:

    猜你喜欢
    • 2021-07-06
    • 2020-12-27
    • 2022-11-27
    • 2021-03-23
    • 2021-08-29
    • 2021-10-02
    • 1970-01-01
    • 2020-12-15
    相关资源
    最近更新 更多