Design Converter
Education
Software Development Executive - II
Last updated on Sep 5, 2024
Last updated on Oct 24, 2023
Welcome to the world of Records in Dart 3.0, a language construct that allows developers to bundle multiple objects into a single object. In this blog, we will focus on the use of Records and its relationship with another data structure, Tuple, within the Flutter framework known as the Dart Tuple.
A Record is an immutable aggregate type. Unlike collection types, which contain homogeneous groups of objects, a Record, on the other hand, is capable of holding fixed-sized, heterogeneous, and typed objects.
1// Example of Record in Dart: 2 3var recordAB = (a: 1, b: 2); 4var recordXY = (x: 3, y: 4); 5 6// Compile error! These records don't have the same type. 7// recordAB = recordXY;
With this flexible record type, developers have the liberty to create records with different field types; a feature that most object-oriented languages don't easily offer.
In Dart, record expressions are simply comma-delimited lists of named or positional fields, enclosed in parentheses. An example of creating var record could look like:
1var record = ('first', a: 2, b: true, 'last');
Dart provides features to create record types that have both named and positional fields. Named fields, which are included inside curly braces in a record type appearance, are crucial for creating a Flutter record. For instance:
1// Record type annotation in a variable declaration: 2({int a, bool b}) record; 3// Initialize it with a record expression: 4record = (a: 123, b: true);
All record fields are accessible via built-in getters. As records are immutable, fields do not have setters. Named fields expose getters of the same name, while positional fields use the name $<position>
, eliminating any ambiguity.
Here's an example:
1var finalRecord = ('first', a: 2, b: true, 'last'); 2print(finalRecord.$1); // Prints 'first' 3print(finalRecord.a); // Prints 2 4print(finalRecord.b); // Prints true 5print(finalRecord.$2); // Prints 'last'
To further streamline record field access, one can apply various patterns provided by the language.
The field types of a record help define its shape. Field types can differ within the same record, with the type system acknowledging each field’s type when the record is accessed.
1(num, Object) pair = (42, 'a'); 2var first = pair.$1; // Static type `num`, runtime type `int`. 3var second = pair.$2; // Static type `Object`, runtime type `String`.
The record type system acknowledges and treats identical records created by two unrelated libraries as being of the same type.
Two records are considered equal if they possess the same shape and corresponding fields bear the same values. Here, the term 'shape' refers to the set of fields, the fields’ types, and their names of a record. An example demonstrating this can be as follows:
1(int x, int y, int z) point = (1, 2, 3); 2(int r, int g, int b) color = (1, 2, 3); 3print(point == color); // Prints 'true'
Records in Dart automatically define hashCode and == methods based on the structure of their fields, enhancing their usability and management.
One remarkable feature of records is their ability to return multiple values from a function. This resourceful feature leverages Dart Tuple to destructure the values into local variables using pattern matching.
1(String, int) userInfo(Map<String, dynamic> json) { 2 return (json['name'] as String, json['age'] as int); 3} 4 5final json = <String, dynamic>{ 6 'name': 'Dash', 7 'age': 10, 8 'color': 'blue', 9}; 10 11// Destructures using a record pattern: 12var (name, age) = userInfo(json);
Though you can return multiple values without records, it often lacks type safety and increases verbosity.
This blog provided a closer examination of Records in Dart 3.0 and their usage in bundling multiple objects into a single object. We've discovered how the Dart Tuple, a variant of Record, and positioning parameters offer a myriad of benefits in structuring our Flutter applications.
The features and utilities showcased offer app developers a lot more flexibility and control, helping them to build more robust and manageable Flutter apps while encapsulating the true power of Dart 3.0.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.