collection is a variable it can save many data for example
- List
- Set
- Mapping
List Example
List index is start from 0
['this str', 10, 15, 2.0]
example this str
is index 0 and 10
is index 1, etc
void main() {
List datas = ['this str', 10, 15, 2.0];
print(datas);
}
Getting All List item
for print all item you can use looping
void main() {
List datas = ['this str', 10, 15, 2.0];
// print all list
for (int i = 0; i < datas.length; i++) {
print(datas[i]);
}
}
if using forEach
void main() {
List datas = ['this str', 10, 15, 2.0];
int i = 0;
datas.forEach((item) {
print('data ke ${i+1} isi data ${item}');
i++;
});
}
pythonic way using for in
void main() {
List datas = ['this str', 10, 15, 2.0];
int i = 0;
for (dynamic data in datas) {
print("data index ${i} ${data}");
i++; // <- index
}
print("Total Data: ${datas.length}");
}
Set
Set is a immutable datatype
void main(List<String> args) {
Set <String> halogens = {'fluorine', 'chlorine', 'bromine', 'iodine', 'astatine'};
}
Concate the Set
We Can Concate the Set Datatype but can’t duplicate the Data
void main() {
Set dummies = {10, 15, 9, 8, 7};
Set dummy = {1, 5, 19, 8, 7};
print(dummies.union(dummy));
}
// result
{10, 15, 9, 8, 7, 1, 5, 19}
Map
map is a data type that has a key and a value
var gifts = {
// Key: Value
'first': 'partridge',
'second': 'turtledoves',
'fifth': 'golden rings'
};
var nobleGases = {
2: 'helium',
10: 'neon',
18: 'argon',
};
another example
Map scores = {
"math": {10, 15, 9, 8, 7},
"social": {1, 5, 19, 8, 7}
};
{math: {10, 15, 9, 8, 7}, social: {1, 5, 19, 8, 7}}
Manual Mapping
manual mapping using looping
main(List<String> args) {
List<String> dataList = [];
List<int> number = [10, 20, 30, 60];
// looping
number.forEach((element) {
dataList.add('angka ke ' + element.toString());
});
// cetak list
dataList.forEach((newElement) {
print(newElement);
});
}
Using Map Function
This is Best Practice for Mapping Data using map()
function
main(List<String> args) {
List<String> dataList = [];
List<int> number = [10, 20, 30, 60];
// looping
number.map((e) => ' angka ' + e.toString());
// cetak list
dataList.forEach((newElement) {
print(newElement);
});
}
Conclusion
The Important DataType for Developing Software Applications is A Collection Datatype it can be used for Get Response data From API or Mapping to Display in Mobile Applications.