Skip to content

Instantly share code, notes, and snippets.

@AlexVegner
Last active January 22, 2020 15:39
Show Gist options
  • Save AlexVegner/93ad37c8b0148a8a669c80d7a52486fe to your computer and use it in GitHub Desktop.
Save AlexVegner/93ad37c8b0148a8a669c80d7a52486fe to your computer and use it in GitHub Desktop.
dart_list_set_map.dart
void main() {
// List
List<int> list1 = [1, 2, 3]; // List<int>
List<int> list3 = []; // Empty list
List<String> list2 = ['', 'sdf', ''];
int item0List1 = list1[0]; // read value
list1[0] = 5; // set value for index = 0
list3 = [
// copy list with (...)
...list1,
4
];
List<int> list4 = list1.map((int e) => e + 1).toList();
List<String> list1CastToStr =
list1.map((int e) => e.toString()).toList().cast<String>();
print(list1CastToStr);
// Sets
Set<String> halogens = {'add', 'chlorine', 'brom'};
print('set length ${halogens.length}');
halogens.add('add'); // add value
print('set length ${halogens.length}');
bool isContainAdd = halogens.contains('add');
// Map
var gifts = {
// Key: Value
'first': 'partridge',
'second': 'turtledoves',
'fifth': 'golden rings'
}; // Map<String, String>
var firstGifts = gifts['first']; // read value by key
gifts['second'] = 'robot'; // set value by key
// List, Sets and Map have simular functionality: length, forEach, map, for ... in
// Use is and collection if.
final listValue = 2;
final aList = [
1,
2,
3,
4,
if (listValue > 0) listValue
]; // Use is and collection if.
final bList = [
10,
30,
...aList,
]; // spread.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment