Flutter 从对象列表创建副本并更改新列表,而不更改原始列表

Flutter 从对象列表创建副本并更改新列表,而不更改原始列表,flutter,dart,Flutter,Dart,我有一个对象列表,我想有一个副本,并在不更改原始对象的情况下更改新对象 List<Comment> manageComment(List<Comment> incomingComments) { List<Comment> finalArr = []; var comments = List.from(incomingComments); while (comments.isNotEmpty) { var comment = comme

我有一个对象列表,我想有一个副本,并在不更改原始对象的情况下更改新对象

List<Comment> manageComment(List<Comment> incomingComments) {
  List<Comment> finalArr = [];
    var comments = List.from(incomingComments);
  while (comments.isNotEmpty) {
    var comment = comments.removeAt(0);
    if (comment.parentId == null) {
      finalArr.add(comment);
    } else {
      for (var i = 0; i < finalArr.length; i++) {
        var el = finalArr[i];
        if (el.commentId == comment.parentId) {
          comment.replyTo = el.user;
          el.children.add(comment);
          break;
        } else {
          for (var j = 0; j < el.children.length; j++) {
            var childEl = el.children[j];
            if (childEl.commentId == comment.parentId) {
              comment.replyTo = childEl.user;
              el.children.add(comment);
              break;
            }
          }
        }
      }
    }
  }
    print(finalArr[0].children);
    return finalArr;
}
List manageComment(List incomingComments){
列表最终值=[];
var注释=列表。来源(incomingComments);
while(comments.isNotEmpty){
var comment=comments.removeAt(0);
if(comment.parentId==null){
最后添加(评论);
}否则{
对于(变量i=0;i
注释类:

class Comment {
  String commentId;
  User user;
  User replyTo;
  String text;
  num date;
  String parentId;
  List<Comment> children;

  Comment({
    this.commentId,
    this.user,
    this.replyTo,
    this.text,
    this.date,
    this.parentId,
    this.children,
  });

  Comment copyWith({
    String commentId,
    User user,
    User replyTo,
    String text,
    num date,
    String parentId,
    List<Comment> children,
  }) {
    return Comment(
      commentId: commentId ?? this.commentId,
      user: user ?? this.user,
      replyTo: replyTo ?? this.replyTo,
      text: text ?? this.text,
      date: date ?? this.date,
      parentId: parentId ?? this.parentId,
      children: children ?? this.children,
    );
  }

  Comment.fromJson(Map json)
      : commentId = json['commentId'],
        text = json['text'],
        parentId = json['parentId'],
        user = User.fromJson(json['user']),
        children = [],
        date = json['date'];
}
类注释{
字符串注释ID;
用户;
用户回复;
字符串文本;
数字日期;
字符串parentId;
列出儿童名单;
评论({
这是我的,
这个用户,
这是我的回答,
这个文本,
这个日期,
这个.parentId,
这个,孩子,
});
评论抄袭({
字符串注释ID,
用户,
用户回复:,
字符串文本,
数字日期,
字符串parentId,
列出儿童,
}) {
回复评论(
commentId:commentId??this.commentId,
用户:用户??此用户,
回答:回答??这个,回答,
text:text??this.text,
日期:日期??此日期,
parentId:parentId??this.parentId,
孩子们:孩子们??这个,孩子们,
);
}
Comment.fromJson(映射json)
:commentId=json['commentId'],
text=json['text'],
parentId=json['parentId'],
user=user.fromJson(json['user']),
儿童=[],
date=json['date'];
}
我尝试了这个,但它也改变了原来的列表


我如何才能做到这一点?

您可以尝试更换
var comments=List.from(incomingComments)


List comments=List()…addAll(incomingComments)


而不是
List finalArr=[]请使用
List finalArr=List()

我找到了这个解决方案,并且有效:

评论类中:

  Comment.clone(Comment source)
      : this.commentId = source.commentId,
        this.user = source.user,
        this.replyTo = source.replyTo,
        this.text = source.text,
        this.date = source.date,
        this.parentId = source.parentId,
        this.children = source.children.map((item) => Comment.clone(item)).toList();
并用此获取副本:

var comments = incomingComments.map((e) => Comment.clone(e)).toList();

您需要复制每个
注释
对象,因为您的列表只包含对每个
注释
对象的引用。因此,当您创建一个新列表时,您只是在创建一个新列表,该列表指向
Comment
对象的相同实例。添加此行comments.removeAt(0);在最后的陈述中,在if Elseca之后,您可以提供一些关于什么是
Comment
的详细信息吗?这是您制作的类还是来自某个包?@julemand101我添加了
Comment
class.@julemand101我添加了答案。但我也会检查你的答案。tnx问题是他正在修改原始列表中的
注释
对象,因为他没有制作任何对象的副本。我看到@julemand101,所以你不只是想要列表的副本,而是一个列表中所有项目的副本list@MathisFouques对的因此,您需要找到一种方法,根据现有的
Comment
对象创建新的
Comment
对象。@julemand101我们让问题所有者判断答案是否有用。如果投稿人彼此投了反对票,这将变成一场丑陋的比赛。这不是stackoverflow的目的。正如我在讨论中读到的,您也不是问题的所有者@julemand101。请遵循stackoverflow指导原则,以便更好地协作。