Dart/Flutter: 将对象列表转换为另一个对象列表

huangapple go评论71阅读模式
英文:

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 = &#39;?&#39;, });
}

class DA_Field_val {
  final int DAFIELDKEY;
  String Status;

  DA_Field_val({
    required this.DAFIELDKEY,
    this.Status = &#39;?&#39;,
  });
}

void main() {
  List&lt;DA_Field&gt; list_DA_Field = [DA_Field(DAFIELDKEY:1,strGROUP:&#39;xyz&#39;, strText: &#39;abc&#39;, Status: &#39;Pass&#39;), 
                                DA_Field(DAFIELDKEY:7,strGROUP:&#39;abc&#39;, strText: &#39;pqr&#39;, Status: &#39;Fail&#39;),];
  List&lt;DA_Field_val&gt; list_DA_Field_val = list_DA_Field.map((DA_Field_item) =&gt; 
                                                         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);
  }
}

huangapple
  • 本文由 发表于 2023年2月27日 19:02:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579624.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定