01 · Start well

Functions collections and readable transformations

Lesson 3 · 90 minutes including practice. Read, run the example, complete the task, then compare your work with the solution.

By the end of this lesson

  • Write small functions with explicit inputs.
  • Transform lists without mutating the source accidentally.
  • Use named parameters to make a call understandable.

Give a function one responsibility

A function should make an intention visible. A function named reorderNames should decide which names need attention; it should not also show a dialog and save a file. Separating those responsibilities gives you a function you can run and test without starting Flutter.

Collections encode different relationships

A List preserves a sequence; a Set represents uniqueness; a Map associates a key with a value. Select one because of the relationship, not because it is the first collection you learned. Two products with the same display name can still be different records, so names are a poor substitute for stable identifiers.

Transform deliberately

where filters values, map changes their representation, and fold combines them. These operations return iterables or values; they do not inherently rewrite the original list. Convert to a list when you need a snapshot. Avoid long chains that conceal the business rule. A short loop is often clearer when several decisions must be explained.

Keep presentation at the edge

The stock rule should compare quantities and thresholds, not formatted strings. Compute the relevant records first, then format their names for the screen. Named parameters make related integers less ambiguous: a call with threshold: 5 communicates more than an unexplained second number.

Worked example

The example below is a runnable Dart file; save it as example.dart and run dart run example.dart.

// standalone
List<String> reorderNames(
  Map<String, int> stock, {required int threshold}
) {
  return stock.entries
      .where((entry) => entry.value <= threshold)
      .map((entry) => entry.key)
      .toList()..sort();
}
void main() {
  final stock = {'Rice': 8, 'Soap': 2, 'Tea': 5};
  print(reorderNames(stock, threshold: 5));
  print(stock['Rice']);
}

Your practical task

Change the input so two records are below the threshold and one is exactly equal. Decide whether equality belongs in the result and document your decision. Add a test case for an empty map.

Worked solution and reasoning — attempt the task first

The stated rule uses <=, so stock exactly equal to the threshold belongs in the reorder list. The example prints [Soap, Tea] and then 8; the source map is unchanged. An empty map returns an empty list. Keep threshold as a named parameter and include equality in the test so a future change from <= to < cannot pass unnoticed.

Check your understanding

Why not use a Set for every product collection?

A Set removes duplicates according to equality. Products may share a name and still be distinct records.

Does map() immediately store a fixed result?

It creates an iterable transformation. Use toList() when you need a stable snapshot rather than a view evaluated during iteration.

Official reference

Read the relevant official documentation. The reference extends this lesson; the exercise and explanation above are original course material.