gpt4 book ai didi

dart - 在 Dart 中克隆新 map

转载 作者:行者123 更新时间:2023-12-03 04:50:59 26 4
gpt4 key购买 nike

我有一个 map Map<String, List<ProductItem>> _productItemsMap;。我已将值传递给新 map ,但是当我修改 map 中的项目时,似乎新旧 map 中的值均已更改。我的目的是克隆具有新引用的新 map 。如何在 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;
}
}

最佳答案

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


}

关于dart - 在 Dart 中克隆新 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61348886/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com