英文:
Dart/Flutter: List of Object to other List of Object
问题
我有一个由构造函数创建的列表的列表,如下所示:
class DA_Field {
final int DAFIELDKEY;
final String strGROUP;
final String strText;
String Status;
DA_Field({
required this.DAFIELDKEY,
required this.strGROUP,
required this.strText,
this.Status = '?',
});
}
现在我想将这个列表的列表复制到另一个列表中,如下所示:
class DA_Field_val {
final int DAFIELDKEY;
String Status;
DA_Field_val({
required this.DAFIELDKEY,
this.Status = '?',
});
}
其中,DAFIELDKEY应该在两个列表中保持相同,Status也应该被复制。
我该如何做到这一点?
英文:
I have a list of a list, created with a constructor, like:
class DA_Field { final int DAFIELDKEY; final String strGROUP; final String strText; String Status;
DA_Field({
required this.DAFIELDKEY,
required this.strGROUP,
required this.strText,
this.Status = '?', });}
now I want to copy this list of list in another List, like:
class DA_Field_val {
final int DAFIELDKEY;
String Status;
DA_Field_val({
required this.DAFIELDKEY,
this.Status = '?',
});}
where DAFIELDKEY should be the same in both Lists, Status should also be copied.
How should I do that?
答案1
得分: 0
以下是翻译好的代码部分:
List<DA_Field_Val> list_DA_Filed_Val = list_DA_Field.map((DA_Field_item) => DA_Field_Val(DAFIELDKEY: DA_Field_item.DAFIELDKEY, Status: DA_Field_item.Status)).toList();
class DA_Field {
final int DAFIELDKEY;
final String strGROUP;
final String strText;
String Status;
DA_Field({
required this.DAFIELDKEY,
required this.strGROUP,
required this.strText,
this.Status = '?',
});
}
class DA_Field_val {
final int DAFIELDKEY;
String Status;
DA_Field_val({
required this.DAFIELDKEY,
this.Status = '?',
});
}
void main() {
List<DA_Field> list_DA_Field = [DA_Field(DAFIELDKEY:1, strGROUP:'xyz', strText: 'abc', Status: 'Pass'),
DA_Field(DAFIELDKEY:7, strGROUP:'abc', strText: 'pqr', Status: 'Fail'),];
List<DA_Field_val> list_DA_Field_val = list_DA_Field.map((DA_Field_item) =>
DA_Field_val(DAFIELDKEY: DA_Field_item.DAFIELDKEY, Status: DA_Field_item.Status)).toList();
for (var newObj in list_DA_Field_val) {
print (newObj.DAFIELDKEY);
print (newObj.Status);
}
}
希望这对您有帮助。如果您需要进一步的翻译或有其他问题,请随时提出。
英文:
You can map a list to another type of list. Something like this:
Creating new list from existing list of DA_Field
List<DA_Field_Val>list_DA_Filed_Val = list_DA_Field.map((DA_Field_item) => DA_Field_Val(DAFIELDKEY: DA_Field_item.DAFIELDKEY, Status: DA_Field_item.Status)).toList();
Full code:
class DA_Field {
final int DAFIELDKEY;
final String strGROUP;
final String strText;
String Status;
DA_Field({
required this.DAFIELDKEY,
required this.strGROUP,
required this.strText,
this.Status = '?', });
}
class DA_Field_val {
final int DAFIELDKEY;
String Status;
DA_Field_val({
required this.DAFIELDKEY,
this.Status = '?',
});
}
void main() {
List<DA_Field> list_DA_Field = [DA_Field(DAFIELDKEY:1,strGROUP:'xyz', strText: 'abc', Status: 'Pass'),
DA_Field(DAFIELDKEY:7,strGROUP:'abc', strText: 'pqr', Status: 'Fail'),];
List<DA_Field_val> list_DA_Field_val = list_DA_Field.map((DA_Field_item) =>
DA_Field_val(DAFIELDKEY: DA_Field_item.DAFIELDKEY, Status: DA_Field_item.Status)).toList();
for (var newObj in list_DA_Field_val) {
print (newObj.DAFIELDKEY);
print (newObj.Status);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论