在dart中克隆新地图

在dart中克隆新地图,dart,Dart,我有一个mapmap\u productItemsMap。我已将该值传递给一个新映射,但当我修改映射中的项目时,新映射和旧映射中的值似乎都发生了更改。我的目的是用新引用克隆一个新地图。如何在dart中实现这一点?谢谢你的帮助。:) 我的ProductItem类 class ProductItem { String _id; String _brandName; String _name; String _pic; double _price; dynamic _qty;

我有一个map
map\u productItemsMap。我已将该值传递给一个新映射,但当我修改映射中的项目时,新映射和旧映射中的值似乎都发生了更改。我的目的是用新引用克隆一个新地图。如何在dart中实现这一点?谢谢你的帮助。:)

我的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;
  }
}
类ProductItem{
字符串_id;
字符串_brandName;
字符串\u名称;
串图;
双倍价格;
动态数量;
字符串_brandId;
列出项目详情;
列出所有颜色;
列出项目条件;
列出项目大小;
字符串get id=>\u id;
字符串get brandName=>\u brandName;
字符串get name=>\u name;
字符串get pic=>\u pic;
双倍获取价格=>\u价格;
动态获取数量=>\u数量;
字符串get brandId=>\u brandId;
List get itemDetails=>\u itemDetails;
List get itemColors=>\u itemColors;
List get itemSizes=>\u itemSizes;
List get itemCondition=>\u itemConditions;
ProductItem(此.\u id,此.\u brandName,此.\u name,此.\u图片,此.\u价格,此.\u数量,此.\u brandId,此.\u itemDetails,此.\u ItemColor,此.\u itemConditions,此.\u itemSizes);
ProductItem.map(动态对象){
这个。_id=obj[“id”];
这个._brandName=obj[“brandName”];
这个._name=obj[“name”];
这个._pic=obj[“pic”];
这个价格=obj[“价格”];
此._数量=对象[“数量”];
这个._brandId=obj[“brandId”];
var tempprdemdetail=obj[“项目详细信息”]作为列表;
这个.u itemDetails=tempprdemdetail.map((i)=>ProductItemDetail.map(i)).toList();
var tempprdemcolors=obj[“颜色”]作为列表;
这个.u itemColors=tempPrdItemColors.map((i)=>ProductItemColor.map(i)).toList();
var tempPrdItemSizes=obj[“大小”]作为列表;
这个.u itemSizes=tempPrdItemSizes.map((i)=>ProductItemSize.map(i)).toList();
var tempprdemconditions=obj[“条件”]作为列表;
这个.u itemConditions=tempPrdItemConditions.map((i)=>ProductItemCondition.map(i)).toList();
//这是._itemDetails=obj[“itemDetails”];
}
映射toMap(){
var map=newmap();
映射[“id”]=\u id;
地图[“品牌名称”]=\u品牌名称;
映射[“名称”]=\u名称;
地图[“图片”]=\u图片;
地图[“价格”]=\u价格;
映射[“数量”]=\u数量;
map[“brandId”]=\u brandId;
映射[“itemDetails”]=\u itemDetails;
映射[“颜色”]=\u项目颜色;
映射[“大小”]=\u itemSizes;
映射[“条件”]=\u项条件;
返回图;
}
}
使用

Map newMap=Map.from(\u productItemsMap)
编辑:

//https://github.com/dart-lang/sdk/issues/35366
类别ProductItem{
最终int id;
最后的字符串名;
ProductItem(this.id,this.name);
字符串toString(){
返回“$id$name”;
}
ProductItem.clone(ProductItem项):此(item.id,item.name);
}
void main(){
映射_productItemsMap={
“1”:[ProductItem(1,'a')],
};
Map shallowMap=Map.from(_productItemsMap);
打印(_productItemsMap.toString());
印刷品(“----”);
//shallowMap['1'][0]=ProductItem(2,'b');
//打印(_productItemsMap.toString());
Map deepMap={};
_productItemsMap.forEach((k,v){
列表项=[];
v、 forEach((item)=>items.add(ProductItem.clone(item));
深度地图[k]=项目;
});
deepMap['1'][0]=ProductItem(2,'b');
打印(_productItemsMap.toString());
}

嗯,你说得对。是这样吗,@GenchiGenbutsu?更喜欢使用
Map.of
(在编译时进行类型检查)而不是
Map.from
(在运行时进行类型检查)。当更改新映射时,旧映射仍会受到影响。您确实创建了一个新的
映射,但您的问题是需要创建新映射,
ProductItem
s的深度复制。是的,我想在@jamesdlin进行深度复制
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());


}