如何在Flutter中将列表值复制到另一个列表 [英] How to copy list values to another list in flutter
本文介绍了如何在Flutter中将列表值复制到另一个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将一个列表的值复制到另一个列表,我使用三个按钮:第一个按钮将值添加到mylist,第二个按钮清除mylist,第三个按钮将值从mynewlist复制到mylist.
I am trying to copy values of one list to another, I use three buttons 1st one to append a value to mylist, second one to clear the mylist, 3rd button to copy values from mynewlist to mylist.
我尝试过
List<String> mylist = [
'Albania',
'Andorra',
'Armenia',
'Austria',
'Azerbaijan',
'Belarus',
'Belgium',
'Albania',
'Andorra',
'Armenia',
'Austria',
'Azerbaijan',
'Belarus',
'Belgium',
];
List<String> mynewlist = [
'Albania',
'Andorra',
'Armenia',
'Austria',
'Azerbaijan',
'Belarus',
'Belgium',
'Albania',
'Andorra',
'Armenia',
'Austria',
'Azerbaijan',
'Belarus',
'Belgium',
];
Padding(
padding: const EdgeInsets.all(5.0),
child: Row(
children: <Widget>[
Expanded(
child: FlatButton(
onPressed: () {
setState(() {
print('clicked 1st');
print(mylist.length);
print(mynewlist.length);
mylist.add('sdsds');
});
},
child: Container(
child: Column(
children: <Widget>[
Image.asset(
'images/bulb.png',
width: 100,
height: 100,
),
Text('bulb')
],
),
),
),
),
Expanded(
child: FlatButton(
onPressed: () {
setState(() {
print('clicked 2nd');
print(mylist.length);
print(mynewlist.length);
//after i set mylist = mynewlist; when i click this button it clears the old and new list.
mylist.removeRange(0, mylist.length);
});
},
child: Container(
child: Column(
children: <Widget>[
Image.asset(
'images/bulb.png',
width: 100,
height: 100,
),
Text('bulb')
],
),
),
),
),
Expanded(
child: FlatButton(
onPressed: () {
setState(() {
print('clicked 3rd');
print(mylist.length);
print(mynewlist.length);
mylist = mynewlist;
});
},
child: Container(
child: Column(
children: <Widget>[
Image.asset(
'images/bulb.png',
width: 100,
height: 100,
),
Text('bulb')
],
),
),
),
),
],
),
)
在第二次我单击第二个按钮时,它可以很好地工作,它清除了mylist和mynewlist.
On the initial time it works perfectly the second time i click the second button it clears the mylist and mynewlist.
如何在不清除新列表的情况下复制第二个列表的值
How can i copy the values of second list without clearing the new new list
推荐答案
使用 myList = List.from(mynewlist);
代替 mylist = mynewlist;
这篇关于如何在Flutter中将列表值复制到另一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文