如何将列表值复制到颤振中的另一个列表 [英] How to copy list values to another list in flutter

查看:18
本文介绍了如何将列表值复制到颤振中的另一个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个列表的值复制到另一个,我使用三个按钮第一个将值附加到 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;

这篇关于如何将列表值复制到颤振中的另一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆