在飞镖中克隆新地图

时间:2020-04-21 16:55:27

标签: dart

我有一张地图Map<String, List<ProductItem>> _productItemsMap;。我已将值传递给新地图,但是当我修改地图中的项目时,似乎新旧地图中的值均已更改。我的目的是克隆具有新参考的新地图。如何在飞镖中实现?谢谢你的帮助。 :)

我的ProductItem类

class ProductItem {
  String _id;
  String _brandName;
  String _name;
  String _pic;
  double _price;
  dynamic _qty;
  String _brandId;
  List<ProductItemDetail> _itemDetails;
  List<ProductItemColor> _itemColors;
  List<ProductItemCondition> _itemConditions;
  List<ProductItemSize> _itemSizes;

  String get id => _id;
  String get brandName => _brandName;
  String get name => _name;
  String get pic => _pic;
  double get price => _price;
  dynamic get qty => _qty;
  String get brandId => _brandId;
  List<ProductItemDetail> get itemDetails => _itemDetails;
  List<ProductItemColor> get itemColors=>_itemColors;
  List<ProductItemSize> get itemSizes=> _itemSizes;
  List<ProductItemCondition> get itemCondition=> _itemConditions;

  ProductItem(this._id, this._brandName, this._name, this._pic, this._price, this._qty, this._brandId, this._itemDetails,this._itemColors,this._itemConditions,this._itemSizes);

  ProductItem.map(dynamic obj) {
    this._id = obj["id"];
    this._brandName = obj["brandName"];
    this._name = obj["name"];
    this._pic = obj["pic"];
    this._price = obj["price"];
    this._qty = obj["qty"];
    this._brandId = obj["brandId"];
    var tempPrdItemDetail=obj["itemDetails"] as List;
    this._itemDetails= tempPrdItemDetail.map((i) => ProductItemDetail.map(i)).toList();
    var tempPrdItemColors=obj["color"] as List;
    this._itemColors= tempPrdItemColors.map((i) => ProductItemColor.map(i)).toList();
    var tempPrdItemSizes=obj["size"] as List;
    this._itemSizes= tempPrdItemSizes.map((i) => ProductItemSize.map(i)).toList();
    var tempPrdItemConditions=obj["condition"] as List;
    this._itemConditions= tempPrdItemConditions.map((i) => ProductItemCondition.map(i)).toList();

//    this._itemDetails = obj["itemDetails"];
  }

  Map<String, dynamic> toMap() {
    var map = new Map<String, dynamic>();
    map["id"] = _id;
    map["brandName"] = _brandName;
    map["name"] = _name;
    map["pic"] = _pic;
    map["price"] = _price;
    map["qty"] = _qty;
    map["brandId"] = _brandId;
    map["itemDetails"] = _itemDetails;
    map["color"] = _itemColors;
    map["size"] = _itemSizes;
    map["condition"] = _itemConditions;
    return map;
  }
}

1 个答案:

答案 0 :(得分:1)

使用Map.from constructor

Map<String, List<ProductItem>> newMap = Map.from(_productItemsMap) 

编辑:

//https://github.com/dart-lang/sdk/issues/35366

class ProductItem {
  final int id;
  final String name;

  ProductItem(this.id, this.name);

  String toString(){
    return '$id $name';
  }

  ProductItem.clone(ProductItem item):this(item.id, item.name);
}

void main() {

 Map<String, List<ProductItem>> _productItemsMap = {
   '1': [ProductItem(1, 'a')],
 };


  Map<String, List<ProductItem>> shallowMap = Map.from(_productItemsMap);

  print(_productItemsMap.toString());
  print('------');
  //shallowMap['1'][0]=ProductItem(2, 'b');
  //print(_productItemsMap.toString());


  Map<String, List<ProductItem>> deepMap = {};

  _productItemsMap.forEach((k,v) { 
    List<ProductItem> items = [];

    v.forEach((item)=> items.add(ProductItem.clone(item)));
    deepMap[k] = items;

    });

   deepMap['1'][0]=ProductItem(2, 'b');
   print(_productItemsMap.toString());


}