gpt4 book ai didi

flutter - 如何在 dart 中复制数组元素

转载 作者:行者123 更新时间:2023-12-03 04:09:54 25 4
gpt4 key购买 nike

如果更改了另一个数组的值,则连接问题(如主数组)已更改。我认为问题在于复制相同的地址,不确定但只是想到它。我从过去 3 个小时开始就尝试过,但无法摆脱它。

请看下图以获得更好的想法。

List<page> _pageList;
List<page> _orderList = [];
_pageList = _apiResponse.result as List<page>;
_orderList.add(_pageList[0].element);
_orderList[0].title = "XYZ"
//--> Here if I change the `_orderList[0].title` then it also change the `title` inside "_pageList"

我们如何防止主数组发生变化?

最佳答案

我在我的一个项目中遇到了同样的问题。我所做的是,使用 json 来编码和解码对象,帮助您复制对象,这样就不会影响主列表。

在代码的第三行之后,进行如下更改

Elements copyObject = Elements.fromJson(_pageList[0].element.toJson());
// First of all you have to convert your object to the Map and and map to original object like above
_orderList.add(copyObject);

希望对您有帮助。

关于flutter - 如何在 dart 中复制数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62830748/

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