How do I combine two lists in Dart?

前端 未结 8 729
有刺的猬
有刺的猬 2021-01-31 00:46

I was wondering if there was an easy way to concatenate two lists in dart to create a brand new list object. I couldn\'t find anything and something like this:

My list:

相关标签:
8条回答
  • 2021-01-31 01:48

    Alexandres' answer is the best but if you wanted to use + like in your example you can use Darts operator overloading:

    class MyList<T>{
      List<T> _internal = new List<T>();
      operator +(other) => new List<T>.from(_internal)..addAll(other);
      noSuchMethod(inv){
        //pass all calls to _internal
      }
    }
    

    Then:

    var newMyList = myList1 + myList2;
    

    Is valid :)

    0 讨论(0)
  • 2021-01-31 01:50

    No need to create a third list in my opinion...

    Use this:

    list1 = [1, 2, 3];
    list2 = [4, 5, 6];
    list1.addAll(list2);
    
    print(list1); 
    // [1, 2, 3, 4, 5, 6] // is our final result!
    
    0 讨论(0)
提交回复
热议问题