Flutter › Dart Language Basics & Syntax
What are Dart records, and how do they let you group together several distinct values of potentially different types without needing to first define an entirely separate dedicated class?
AdvancedAnswer
A record is a lightweight, anonymous data structure that lets you group together several distinct individual values, potentially of entirely different types, into one single combined value, without needing to first formally define an entirely separate dedicated class purely to represent that specific grouped shape of data, which is especially convenient for situations like a function that genuinely needs to return two or more distinct related values together, avoiding the overhead of defining a small dedicated wrapper class solely for that one single specific purpose.
(double, double) getScreenDimensions() {
return (400.0, 800.0);
}
final (width, height) = getScreenDimensions();Real-world example A utility function calculating both a discounted price and the total amount actually saved returns both of those distinct values together as a single record, avoiding the need to define an entirely separate small dedicated class purely to hold just those two specific related values.