Bidev
IntermediateDart Questions

What are Records in Dart and when would you use one?

Short Answer

A Record is a built-in, anonymous, immutable data structure that groups multiple values together — useful for returning multiple values from a function without declaring a dedicated class.

Before Records (Dart 3), returning 'two values' from a function meant either creating a small class, using a List/Map (losing type safety), or a Tuple package. Records solve this natively: (int, String) parseEntry() => (200, 'OK'); returns a record you can destructure with pattern matching: final (code, message) = parseEntry();

Records also support named fields — ({int code, String message}) — for clarity when position alone isn't descriptive. Unlike classes, Records are structurally typed and compared by value, not identity, making them convenient for lightweight groupings that don't need custom behavior or identity semantics.

Code Example

(double, double) minMax(List<double> values) {
  return (values.reduce(min), values.reduce(max));
}

final (lowest, highest) = minMax([3, 1, 4, 1, 5]);
print('Range: \$lowest to \$highest');

Common Mistakes

  • ×Reaching for a Record when a proper class with named, semantically meaningful fields and methods would communicate intent better.
  • ×Forgetting Records are compared structurally by value, which is usually desired but can surprise if identity was expected.