A single variable stores one value. Collections keep several related values. List preserves order and permits duplicates. Set contains each value at most once. Map associates a value with a key. Your choice changes which rules the data structure already expresses.
01Keep ordered entries
List<int> means a list of integers. add appends a value. where checks each value and returns a filtered sequence. toList turns it back into a list. Filtering this way retains the original list.
void main() {
final readings = <int>[64820, 64920];
readings.add(0);
print(readings.where((value) => value > 0).toList());
final activities = <String>{'repair', 'service', 'repair'};
print(activities);
final costs = <String, int>{'repair': 12990};
print(costs['repair']);
}
Run / check
dart run collections.dart02Express uniqueness
The <String>{...} form creates a set. The duplicate repair value appears only once. This suits selectable activities when a label should not appear several times.
03Look up a key
In costs, names correspond to amounts in cents. costs["repair"] reads a value. An unknown key returns null, so you must not assume that every lookup finds a number.
Filtered: [64820, 64920]. Activities: {repair, service}. Cost: 12990.
Your exercise
Sum all values in readings. How does this differ from the latest mileage?
Show the solution and explanation
Start with var total = 0 and add each value inside a for-in loop. The result is 129740. Mileage readings are cumulative values. Their sum is not total distance travelled.
Project context and sources
The example is simplified for learning. The linked source version shows its use in the project and the checks applied there.