Statement:

var list1 = List(); List2 = List(); list2 = List(); List3 = List(2); list3 = List(2); List1.add (1); list1.add(1); list1.add('aaa'); list1.add(true); print(list1); //[1, aaa, true]Copy the code
Var list4 = [1, 2, 3]; List5 = [2,'3',true]; list5 = [2,'3',true]; List6 = ['a',2,'b',false]; list6 = ['a',2,'b',false]; List5 [1] = 2; list5[1] = 2; list5[1] = 2; list5[1] = 2; list5[2] = 'aa'; print(list5); //[2, 2, aa]Copy the code
var list33 = <String>["a","b"]; List<String> list7 = ['a','b','3']; List<String> list8 = new List(2); list8[0]=('aaa'); print(list8); //[aaa, null]Copy the code

Commonly used attributes

List<String> list13 = List(); list13.add('aaa'); list13.add('bbb'); list13.add('ccc'); print(list13.length); / / 3 length print (list13. IsEmpty); // If false is empty print(list13.isnotempty); //true Is not empty print(list13.reversed); //(CCC, BBB, aaa) returns an inverted List Iterable without changing the original List print(list13.first); Print (list13.last); // the last element of CCCCopy the code

Commonly used method

Add () adds an element to the end of the List

List<String> list9 = List(); list9.add('aaa'); list9.add('bbb'); print(list9); //[aaa, bbb]Copy the code

AddAll () merges two lists

List<String> list10 = List(); list10.add('aaa'); list10.add('bbb'); List<String> list11 = List(); list11.add('ccc'); list11.add('ddd'); list11.addAll(list10); print(list11); //[ccc, ddd, aaa, bbb]Copy the code

Insert (index,element) Inserts a value at the specified index

List<int> list21 = List(); list21.add(0); list21.add(1); list21.add(2); list21.add(3); print(list21); //[0, 1, 2, 3] list21.insert(1, 5); Print (list21); // [0, 5, 1, 2, 3]Copy the code

InsertAll (index,list) Inserts a list at the specified index

List<int> list25 = List(); list25.add(0); list25.add(1); list25.add(2); list25.add(3); print(list25); //[0, 1, 2, 3] list25.insertAll(1, [5,6,7]); print(list25); //[0, 5, 6, 7, 1, 2, 3]Copy the code

FollowedBy (list) combines itself with a list of arguments

List<int> list45 = [3, 4, 1, 2, 5]; Iterable < int > list46 = list45. FollowedBy ([6, 7]); // print(list46); //(3, 4, 1, 2, 5, 6, 7) // print(list46.toList()); //[3, 4, 1, 2, 5, 6, 7] // print(list46.toList(growable: false)); //[3, 4, 1, 2, 5, 6, 7] Growable =false Indicates that the length of the generated List is fixed and new elements cannot be addedCopy the code

Remove (obj) removes the specific element removeAt(index) Removes the index location element

List<String> list15 = List(); list15.add('aaa'); list15.add('bbb'); list15.add('ccc'); print(list15); //[aaa, bbb, ccc] list15.remove('bbb'); Print (list15); // [aaa, ccc] list15.removeAt(1); Print (list15); //[aaa]Copy the code

RemoveLast () removes the last element removeRange(start,end) range removes removeWhere() according to the condition

List<String> list16 = List(); list16.add('aaa'); list16.add('bbb'); list16.add('ccc'); list16.add('ddd'); list16.add('eee'); list16.add('fff'); print(list16); //[aaa, bbb, ccc, ddd, eee, fff] list16.removeLast(); Print (list16); //[aaa, bbb, ccc, ddd, eee] list16.removeRange(0, 2); Print (list16); print(list16); //[ccc, ddd, eee] list16.removeWhere((element){ return element=='ddd'; }); Print (list16); print(list16); //[ccc, eee] list16.removeWhere((element)=> element=='ccc'); If there is only one expression, print(list16) can be simplified with the arrow function; //[eee]Copy the code

Clear () clears the array

List<String> list17 = List(); list17.add('aaa'); list17.add('bbb'); list17.add('ccc'); list17.add('ddd'); print(list17); //[aaa, bbb, ccc, ddd] list17.clear(); // Empty array print(list17); / / []Copy the code

Modifies the value of the specified index position

var list12 = [2,'3',true]; list12[1] = 2; print(list12); // [2, 2, true]Copy the code

SetRange (startIndex,endIndex,list) range Changes the list value

List<String> list18 = List(); list18.add('aaa'); list18.add('bbb'); list18.add('ccc'); list18.add('ddd'); // print(list18); // [aaa, bbb, ccc, ddd] List<String> list19 = List(); list19.add("111"); list19.add("222"); list19.add("333"); list19.add("444"); list19.add("555"); list19.add("666"); List18. SetRange (0, 3, list19); // Select * from list19; select * from list18; select * from list19; select * from list18; Start and end Any corner beyond list18 list19 will report an error. // print(list18); //[111, 222, ccc, ddd]Copy the code

ReplaceRange (start,end,list) range to replace a header without a tail

List<int> list27 = List(); list27.add(0); list27.add(1); list27.add(2); list27.add(3); print(list27); / / [0, 1, 2, 3]. List27 replaceRange (1, 3,,6,7,8 [5]); Print (list27); //[0, 5, 6, 7, 8, 3]Copy the code

FillRange (start,end,value) Replace each element from start-end with value

List<int> list30 = List(); list30.add(0); list30.add(1); list30.add(2); list30.add(3); list30.add(4); // print(list30); //[0, 1, 2, 3, 4] list30. FillRange (1, 4,6); Print (list30); // Print (list30); //[0, 6, 6, 6, 4]Copy the code

RetainWhere (()=>(bool)) Filters elements based on conditions

List<int> list31 = List(); list31.add(0); list31.add(1); list31.add(2); list31.add(3); list31.add(4); // print(list31); //[0, 1, 2, 3, 4] list31.retainWhere((element)=>(element>2)); // print(list31); / / [3, 4]Copy the code

SetAll (index,list) Starts at index and replaces each element in the list one by one

List<int> list32 = List(); list32.add(0); list32.add(1); list32.add(2); list32.add(3); list32.add(4); // print(list32); //[0, 1, 2, 3, 4] list32.setall (2, [7,6]); Index +arr.length must <=list32.length otherwise error: // print(list32); //[0, 1, 7, 6, 4]Copy the code

View the value of the index position

List<String> list14 = List(); list14.add('aaa'); list14.add('bbb'); list14.add('ccc'); print(list14[0]); Print (list14[2]); //ccc print(list14.first); Print (list14.last); print(list14.last); // CCC gets the last element easilyCopy the code

IndexOf (element,[start]) finds the indexOf the specified element in the list

List<int> list24 = List(); list24.add(0); list24.add(1); list24.add(2); list24.add(3); int index1 = list24.indexOf(2); Int index2 = list24.indexof (2,3); Print (index1); print(index1); //2 print(index2); / / 1Copy the code

LastIndexOf (obj,index) returns the first index by looking backwards

List<String> list34 = List(); list34.add("aaa"); list34.add("bbb"); list34.add("ccc"); list34.add("ddd"); list34.add("eee"); // print(list34); // [aaa, bbb, ccc, ddd, eee] int index = list34.lastIndexOf("ccc",1); Print (index); print(index); //-1 int index3 = list34.lastIndexOf("ccc",2); Print (index3); print(index3); / / 2Copy the code

ElementAt (index) gets the elementAt the specified index position

List<String> list40 = List(); list40.add("aaa"); list40.add("bbb"); list40.add("ccc"); list40.add("ddd"); list40.add("eee"); // print(list40); // [aaa, bbb, ccc, ddd, eee] String result3 = list40.elementAt(4); // print(result3); //eeeCopy the code

Any ((element)=>(bool)) Checks whether any element in the List matches the given parameter

List<String> list36 = List(); list36.add("aaa"); list36.add("bbb"); list36.add("ccc"); list36.add("ddd"); list36.add("eee"); // print(list36); // [aaa, bbb, ccc, ddd, eee] bool result = list36.any((element)=>(element=="d")); // Print (result); // Print (result); //falseCopy the code

Every ((element)=>(bool)) determines whether every element in the List conforms to the argument function

List<int> list41 = [1, 2, 3, 4, 5]; bool result4 = list41.every((element)=>(element%2==0)); Return true otherwise return false bool result5 = list41. Every ((element)=>(element>0)); // print(result4); //false // print(result5); //trueCopy the code

Contains (obj) List whether the given OBj exists

List<String> list39 = List(); list39.add("aaa"); list39.add("bbb"); list39.add("ccc"); list39.add("ddd"); list39.add("eee"); // print(list39); // [aaa, bbb, ccc, ddd, eee] bool result2 = list39.contains("eee"); // print(result2); //trueCopy the code

FirstWhere ((element)=>(bool)) returns the first element that meets the condition (not the element’s index)

List<int> list43 = [1, 2, 3, 4, 5]; int result7 = list43.firstWhere((element)=>(element>2)); Result8 = list43.firstWHERE ((element)=>(element>44)); Int result9 = list43.firstWHERE ((element)=>(element>44),orElse: ()=>(10)); // print(result7); // print(result7); //3 // print(result8); // print(result9); / / 10Copy the code

IndexWhere((e)=>(bool)) Returns the index of the first element that meets the condition

List<int> list48 = [3, 4, 1, 2, 5]; int result12 = list48.indexWhere((e)=>(e>3)); // return index // print(result12); //1 int result14 = list48.indexWhere((e)=>(e>3),2); // print(result14); // print(result14); //4 int result13= list48.indexWhere((e)=>(e>13)); If the index of the first element does not exist, return -1 // print(result13); //-1 int result15= list48.indexWhere((e)=>(e<2),3); // print(result15); // return the index of the first element that meets the criteria. //-1 int result16 = list48.lastIndexWhere((e)=>(e>3)); // print(result16); //4 int result17 = list48.lastIndexWhere((e)=>(e>3),1); // print(result17); // print(result17); //1 int result18= list48.lastIndexWhere((e)=>(e>13)); // print(result18); // print(result18); //-1 int result19= list48.lastIndexWhere((e)=>(e<2),3); // print(result19); // print(result19); / / 2Copy the code

LastWhere ((e)=>(bool)) returns the value of the first element that meets the condition.

List<int> list49 = [3, 4, 1, 2, 5]; int result20 = list49.lastWhere((e)=>e>2); Print (result20); //print(result20); //5 //int result22 = list49.lastWhere((e)=>e>22); Int result21 = list49.lastWHERE ((e)=>e>11,orElse: ()=>(44)); //print(result21); // Print (result21); / / 44Copy the code

ForEach () List iterates through each element

List<int> list47 = [3, 4, 1, 2, 5]; List47. ForEach ((element){if (element += 1){if (element += 1){list47. // print(element); List47 [3]=0; list47[3]=0; list47[3]=0; List47 [0]=0; }); // print(list47); For (var x in list47){print(x); //0 4 1 0 5}Copy the code

Map () iterates through each element of the existing List and returns a new Iterable

List<int> list51 = [3, 4, 1, 2, 5]; Iterable<String> result25 = list51.map((e)=>(e>2?" a":"b")); Print (result25); // Print (result25); //(a, a, b, b, a) Iterable<bool> result26 = list51.map((e)=>(e>2)); // print(result26); //(true, true, false, false, true)Copy the code

fold(initValue,(preValue,element)=>()); Based on the existing List and the given initValue, specify a parameter function rule that operates on each element of the List and returns the result.

List<int> list44 = [1, 2, 3, 4, 5]; int result10 = list44.fold(2, (a,element)=>(a*element)); //2*(1*2*3*4*5) 2 is the initial value. The following methods define the operation between the initial value and List and return the result. // print(result10); //240 int result11 = list44.fold(2, (a,element)=>(a+element)); //2+(1+2+3+4+5) = 17 // print(result11); / / 17Copy the code

Reduce ((a,b)=>(some operation)) performs continuous operations on elements in the specified method and returns the result

List<int> list52 = [3, 4, 1, 2, 5]; int result27 = list52.reduce((a,b)=>(a+b)); Print (result27); // print(result27); //15 int result28 = list52.reduce((a,b)=>(a*b)); Print (result28); // print(result28); / / 120Copy the code

Skip (count) Skip (count) Iterable skipWhile((e)=>(bool)) find the first unqualified element and return it and all subsequent elements

List<int> list54 = [3, 4, 1, 2, 5]; Iterable<int> result30 = list54.skip(2); // print(result30); //(1, 2, 5) Iterable<int> result31 = list54.skip(3); // print(result31); //(2, 5) Iterable<int> result32 = list54.skipWhile((e)=>(e>2)); Print (result32.tolist ()); print(result32.tolist ()); //[1, 2, 5] Iterable<int> result35 = list54.skipWhile((e)=>(e<4)); Print (result35.tolist ()); print(result35.tolist ()); //[4, 1, 2, 5] Iterable<int> result36 = list54.skipWhile((e)=>(e>0)); // print(result36.tolist ()); // Print (result36.tolist ()); //[] Iterable<int> result37 = list54.skipWhile((e)=>(e<0)); // Start with the first element and determine whether the argument function meets the requirements one by one until the first element fails. None of them actually means the first one doesn't work, so return the first one and the next one. // print(result37.toList()); //[3, 4, 1, 2, 5]Copy the code

TakeWhile ((e)=>(bool)) take(count) from 0 and return the result until the first element does not match the function.

List<int> list55 = [3, 4, 1, 2, 5]; Iterable<int> result33 = list55.take(2); Print (result33); // Print (result33); //(3, 4, 1) Iterable<int> result34 = list55.takeWhile((e)=>(e>2)); Print (result34); print(result34); / / (3, 4)Copy the code

Where ((e)=>(bool) Filters each element according to the specified parameter function. The elements that meet the conditions form a new Iterable

List<int> list57 = [3, 4, 1, 2, 5,2,3,6]; Iterable<int> result39 = list56.where((e)=>(e>2)); //print(result39); //(3, 4, 5, 3, 6)Copy the code

SingleWhere ((e)=>(bool>

List<int> list53 = [3, 4, 1, 2, 5]; int result29 = list53.singleWhere((e)=>(e>4),orElse: ()=>(10)); // Find the only element that meets the condition. If there are no or more elements that meet the condition, return the value of the orElse method. If no orElse is passed, an error is reported. // print(result29); / / 5Copy the code

WhereType () filters data of the specified type from a List without the specified generic type.

List list58 = [3, 4, "a",true,"b",5,false]; Iterable<int> result40 = list58.whereType(); Print (result40); print(result40); //(3, 4, 5) Iterable<String> result41 = list58.whereType(); print(result41); //(a, b) Iterable<bool> result42 = list58.whereType(); print(result42); //(true, false)Copy the code

Cast () elevates the generics of List to its parent class

List<String> list37 = List(); list37.add("aaa"); list37.add("bbb"); list37.add("ccc"); list37.add("ddd"); list37.add("eee"); // print(list37); // [aaa, bbb, ccc, ddd, eee] List<Object> list38 = list37.cast(); List<Object> list38.add("222"); list38.add("222"); Add (2) print(list38); //[aaa, bbb, ccc, ddd, eee, 222]Copy the code

Expand () specifies a rule to generate a new List based on the existing List

List<int> list42 = [1, 2, 3, 4, 5]; Iterable<int> result6 = list42.expand((element)=>([element+1,element+2])); Print (result6); // print(result6); //(2, 3, 3, 4, 4, 5, 5, 6, 6, 7) // print(result6.toList()); //[2, 3, 3, 4, 4, 5, 5, 6, 6, 7]Copy the code

ToSet () converts a List to a Set to remove repeated elements

List<int> list56 = [3, 4, 1, 2, 5,2,3,6]; Set<int> result38 = list56.toSet(); //print(result38); //{3, 4, 1, 2, 5, 6}Copy the code

AsMap () converts a list to a map

List<String> list33 = List(); list33.add("aaa"); list33.add("bbb"); list33.add("ccc"); list33.add("ddd"); list33.add("eee"); // print(list33); // [aaa, bbb, ccc, ddd, eee] Map<int,String> map = list33.asMap(); Key = index value = list // print(map); //{0: aaa, 1: bbb, 2: ccc, 3: ddd, 4: eee}Copy the code

Shuffle () The elements in the shuffle() List are sorted randomly

List<String> list35 = List(); list35.add("aaa"); list35.add("bbb"); list35.add("ccc"); list35.add("ddd"); list35.add("eee"); // print(list35); // [aaa, bbb, ccc, ddd, eee] list35.shuffle(); // print(list35); //[ddd, eee, aaa, ccc, bbb]Copy the code

Sort () List sorts itself

List<int> list20 = List(); list20.add(2); list20.add(3); list20.add(1); list20.add(0); // print(list20); //[2, 3, 1, 0] list20.sort((a,b)=>(a>b? 1:1)); Print (list20); print(list20); / / [0, 1, 2, 3]Copy the code

Sublist (start,[end]) intercepts the list from the specified index

List<int> list21 = List(); list21.add(0); list21.add(1); list21.add(2); list21.add(3); print(list21); //[0, 1, 2, 3] list21.insert(1, 5); Print (list21); // [0, 5, 1, 2, 3] List<int> list22 = list21.sublist(1); List<int> list23 = list21.sublist(1,3); Print (list22); print(list22); //[5, 1, 2, 3] print(list23); / / (5, 1)Copy the code

GetRange (start,end) intercepts data in the start-end range from the list

List<int> list28 = List(); list28.add(0); list28.add(1); list28.add(2); list28.add(3); list28.add(4); list28.add(5); list28.add(6); list28.add(7); // print(list28); //[0, 1, 2, 3, 4, 5, 6, 7] Iterable<int> list29 = list28.getRange(2, 5); // Print (list29); // Print (list29); / / (2, 3, 4)Copy the code

Join (“-“) joins each element in the List with the specified character and returns a string

List<int> list50 = [3, 4, 1, 2, 5]; String result24 = list50.join("-"); Print (result24); // Print (result24); / / 3-4-1-5Copy the code