Common attributes and methods of list and map in Dar

list common properties and methods

common property

1,length gets the length of list

main() {
  var list = ['red','yellow','pink'];
  print(list.length);
}

2, judge whether it is empty

main() {
  var list = ['red','yellow','pink'];
  print(list.isEmpty);
}

3, not null

main() {
  var list = ['red','yellow','pink'];
  print(list.isNotEmpty);
}

4, the array flips

main() {
  var list = ['red','yellow','pink'];
  print(list.reversed.toList());
}
(pink, yellow, red)

common method

1, add data

main() {
  var list = ['red','yellow','pink'];
  list.add("green");
  print(list);
}
[red, yellow, pink, green]

2, find data indexOf, find return index, can’t find return -1

main() {
  var list = ['red','yellow','pink'];
  var res = list.indexOf('yellow');
  print(res);

}  

1

3, removed data (‘ value ‘),removeAt(index)

main() {
  var list = ['red','yellow','pink'];
  list.remove('yellow');
  list.removeAt(1);

  print(list);

}  
[red]

4, modify data fillRange(start,end,value) without end

main() {
  var list = ['red','yellow','pink'];
  list.fillRange(1, 2,'green');
  print(list);

}

[red, green, pink]

5, insert data insert(index,value) to the specified position. Insert

from index

main() {
  var list = ['red','yellow','pink'];
  list.insert(1, 'green');
  print(list);
}

[red, green, yellow, pink] 

main() {
  var list = ['red','yellow','pink'];
  list.insertAll(1, ['green','white']);
  print(list);
  
} 
[red, green, white, yellow, pink]

6, convert to string join(‘, ‘), instead of the original list

main() {
  var list = ['red','yellow','pink'];
  var str = list.join(',');
  print(str);

}
 
red,yellow,pink

7, convert string to array split(‘ – ‘), do not change the original string

main() {
  var str = 'red-yellow-pink';
  var list = str.split('-');
  print(list);
  print(str);
  
}  
[red, yellow, pink]
red-yellow-pink

8, for(var item in list) loop list

main() {
  var list = ['red','yellow','pink'];
  for(var item in list) {
    print(item);
  }
}

red
yellow
pink

9,map loop list

main() {
  var list = [1,2,3,4];
  var newList = list.map((value) => value * 2);
  print(newList.toList());
}

10, see if the set satisfies some condition any

main() {
  var list = ['red','yellow','pink'];
  var f = list.any((value) => value == 'red');
  print(f);
  print(list);
  
}

11, through the set can not add repeated collection, so can not get

through the index

main() {
  var set = new Set();
  set.add('red');
  set.add('yellow');
  set.add('pink');
  set.add('red');
  print(set.toList());
}  

[red, yellow, pink]

maps type common properties and methods

1, get all keys

main() {
  var person = {
    'name':'张三',
    'age':20,
  };
  print(person.keys.toList());
}

[name, age]

2, get all values

main() {
  var person = {
    'name':'张三',
    'age':20,
  };
  print(person.values.toList());
}

3, determine whether the attribute isEmpty isEmpty

main() {
  var person = {
    'name':'张三',
    'age':20,
  };
  print(person.isEmpty);
  
}
false

common method

1, remove the specified key data (key)

main() {
  var person = {
    'name':'张三',
    'age':20,
  };
  person.remove('name');
  print(person);
}
{age: 20}

2, addAll({‘ key ‘:’ value ‘})

main() {
  var person = {
    'name':'张三',
    'age':20,
  };
  person.addAll({
    'sex':'男',
  });
  print(person);
}

{name: 张三, age: 20, sex: 男}

3, check to see if the fields are in the map containsKey(key), return true/false does not change the original pair like

main() {
  var person = {
    'name':'张三',
    'age':20,
  };
  var boo = person.containsKey('name');
  print(boo);

}

true

blog address
related documents
github document address

Read More: