### Dart Records Example Source: https://github.com/google/tuple.dart/blob/master/README.md Demonstrates the basic usage of Dart Records, which serve similar use cases to package:tuple. ```dart var record = (123, true); print('${record.$1}: ${record.$2}'); ``` -------------------------------- ### Install tuple package Source: https://context7.com/google/tuple.dart/llms.txt Add the tuple package to your pubspec.yaml file to include it in your project. ```yaml dependencies: tuple: ^2.0.2 ``` -------------------------------- ### Tuple2 Usage Example Source: https://context7.com/google/tuple.dart/llms.txt Demonstrates construction, copy-with, fromList, toList, and equality for Tuple2. Tuples can be used as Map keys. ```dart import 'package:tuple/tuple.dart'; void main() { // Construction const point = Tuple2('x', 42); print(point.item1); // x print(point.item2); // 42 print(point); // [x, 42] // Copy-with (returns a new immutable tuple) final moved = point.withItem2(100); print(moved); // [x, 100] print(point); // [x, 42] — original unchanged // From list (throws ArgumentError if length != 2) final parsed = Tuple2.fromList(['y', 7]); print(parsed); // [y, 7] // To list (fixed-length by default) final list = point.toList(); print(list); // [x, 42] // list.add('z'); // throws UnsupportedError — fixed length final growable = point.toList(growable: true); growable.add('z'); print(growable); // [x, 42, z] // Equality & use as Map key const a = Tuple2('x', 42); const b = Tuple2('x', 42); print(a == b); // true final cache = , String>{}; cache[a] = 'hit'; print(cache[b]); // hit } ``` -------------------------------- ### Tuple3 Usage Example Source: https://context7.com/google/tuple.dart/llms.txt Illustrates Tuple3 construction, immutable updates with copy-with, conversion from a list, and equality checks. Suitable for representing data like RGB colors. ```dart import 'package:tuple/tuple.dart'; void main() { // RGB color representation const red = Tuple3('red', 255, 0); print(red.item1); // red print(red.item2); // 255 print(red.item3); // 0 print(red); // [red, 255, 0] // Immutable update final orange = red.withItem3(165); print(orange); // [red, 255, 165] // From list final green = Tuple3.fromList(['green', 0, 128]); print(green); // [green, 0, 128] // Equality print(red == Tuple3('red', 255, 0)); // true print(red == orange); // false // Map key final palette = , String>{}; palette[red] = '#FF0000'; print(palette[Tuple3('red', 255, 0)]); // #FF0000 } ``` -------------------------------- ### Tuple4 Usage Example Source: https://context7.com/google/tuple.dart/llms.txt Shows how to use Tuple4 for bundling four typed values, including construction, updating with copy-with, and conversion from a list. Useful for representing structured data like database records. ```dart import 'package:tuple/tuple.dart'; void main() { // Database record: (id, name, score, active) const record = Tuple4(1, 'Alice', 98.5, true); print(record.item1); // 1 print(record.item2); // Alice print(record.item3); // 98.5 print(record.item4); // true print(record); // [1, Alice, 98.5, true] // Deactivate record final deactivated = record.withItem4(false); print(deactivated); // [1, Alice, 98.5, false] // From list final r2 = Tuple4.fromList([2, 'Bob', 72.0, true]); print(r2); // [2, Bob, 72.0, true] // toList print(record.toList()); // [1, Alice, 98.5, true] } ``` -------------------------------- ### Tuple2 Initialization and Access Source: https://github.com/google/tuple.dart/blob/master/README.md Shows how to initialize a Tuple2 with specific types and access its elements using item1 and item2. ```dart const t = Tuple2('a', 10); print(t.item1); // prints 'a' print(t.item2); // prints '10' ``` -------------------------------- ### Using Tuple7 in Dart Source: https://context7.com/google/tuple.dart/llms.txt Shows how to use Tuple7 for server log entries, including element access, modification via `withItem` methods, construction from a list, and usage as a map key. ```dart import 'package:tuple/tuple.dart'; void main() { // Server log entry: (timestamp, host, port, method, path, status, latencyMs) const entry = Tuple7( '2024-11-15T10:30:00Z', 'api.example.com', 443, 'GET', '/health', 200, 12.5); print(entry.item1); // 2024-11-15T10:30:00Z print(entry.item2); // api.example.com print(entry.item3); // 443 print(entry.item4); // GET print(entry.item5); // /health print(entry.item6); // 200 print(entry.item7); // 12.5 print(entry); // [2024-11-15T10:30:00Z, api.example.com, 443, GET, /health, 200, 12.5] // Record a slow response final slow = entry.withItem7(3500.0).withItem6(503); print(slow); // [2024-11-15T10:30:00Z, api.example.com, 443, GET, /health, 503, 3500.0] // From list final e2 = Tuple7.fromList( ['2024-11-15T11:00:00Z', 'api.example.com', 443, 'POST', '/data', 201, 88.0]); print(e2.toList()); // [2024-11-15T11:00:00Z, api.example.com, 443, POST, /data, 201, 88.0] // Use as map key final seen = , int>{}; seen[entry] = 1; print(seen[entry]); // 1 } ``` -------------------------------- ### Using Tuple5 in Dart Source: https://context7.com/google/tuple.dart/llms.txt Demonstrates creating a Tuple5 to hold HTTP request metadata and accessing its elements. Shows how to modify elements using `withItem` methods and construct from a list. ```dart import 'package:tuple/tuple.dart'; void main() { // HTTP request metadata: (method, path, statusCode, duration, cached) const req = Tuple5( 'GET', '/api/users', 200, 45.3, true); print(req.item1); // GET print(req.item2); // /api/users print(req.item3); // 200 print(req.item4); // 45.3 print(req.item5); // true print(req); // [GET, /api/users, 200, 45.3, true] // Log a cache miss final miss = req.withItem5(false).withItem4(120.7); print(miss); // [GET, /api/users, 200, 12.5, false] // From list final r2 = Tuple5.fromList( ['POST', '/api/users', 201, 88.1, false]); print(r2.toList()); // [POST, /api/users, 201, 88.1, false] } ``` -------------------------------- ### Using Tuple6 in Dart Source: https://context7.com/google/tuple.dart/llms.txt Illustrates the creation and usage of a Tuple6 for calendar event data. Includes accessing elements, modifying them with `withItem` methods, and constructing from a list. ```dart import 'package:tuple/tuple.dart'; void main() { // Calendar event: (id, title, year, month, day, allDay) const event = Tuple6( 42, 'Conference', 2024, 11, 15, true); print(event.item1); // 42 print(event.item2); // Conference print(event.item3); // 2024 print(event.item4); // 11 print(event.item5); // 15 print(event.item6); // true print(event); // [42, Conference, 2024, 11, 15, true] // Reschedule final rescheduled = event.withItem5(22).withItem6(false); print(rescheduled); // [42, Conference, 2024, 11, 22, false] // From list final e2 = Tuple6.fromList( [43, 'Workshop', 2024, 12, 1, false]); print(e2.toList()); // [43, Workshop, 2024, 12, 1, false] } ``` -------------------------------- ### Import tuple package Source: https://context7.com/google/tuple.dart/llms.txt Import the tuple package into your Dart files to use its functionalities. ```dart import 'package:tuple/tuple.dart'; ``` -------------------------------- ### Constructing Tuples from Lists in Dart Source: https://context7.com/google/tuple.dart/llms.txt Explains the `TupleN.fromList` factory constructor, including successful usage with correct length and types, and error handling for incorrect length (ArgumentError) and type mismatches (TypeError/CastError). Provides a typical use case for deserializing data. ```dart import 'package:tuple/tuple.dart'; void main() { // Success — correct length and types final t = Tuple3.fromList([1, 'hello', true]); print(t); // [1, hello, true] // Wrong length — throws ArgumentError try { Tuple3.fromList([1, 'hello']); } on ArgumentError catch (e) { print('Length error: $e'); // Length error: Invalid argument(s): items must have length 3 } // Wrong type — throws TypeError/CastError at runtime try { Tuple2.fromList([1, 99]); // 99 is not a String } catch (e) { print('Type error: $e'); } // Typical use: deserialising JSON or database rows final row = [42, 'Alice', 98.6]; final record = Tuple3.fromList(row); print('${record.item2} scored ${record.item3}'); // Alice scored 98.6 } ``` -------------------------------- ### Convert Tuple to List Source: https://context7.com/google/tuple.dart/llms.txt Use `toList()` to convert a tuple into a `List`. By default, it creates a fixed-length list. Pass `growable: true` for a resizable list. This is useful for JSON serialization. ```dart import 'package:tuple/tuple.dart'; void main() { const t = Tuple4('Alice', 30, 5.7, true); // Fixed-length (default) final fixed = t.toList(); print(fixed); // [Alice, 30, 5.7, true] print(fixed.length); // 4 try { fixed.add('extra'); // throws UnsupportedError } on UnsupportedError catch (e) { print('Cannot grow: $e'); } // Growable list final growable = t.toList(growable: true); growable.add('extra'); print(growable); // [Alice, 30, 5.7, true, extra] // Useful for JSON serialisation final json = t.toList(growable: false).map((e) => e.toString()).toList(); print(json); // [Alice, 30, 5.7, true] } ``` -------------------------------- ### Tuple2 withItem1 Modification Source: https://github.com/google/tuple.dart/blob/master/README.md Illustrates creating a new Tuple2 object with one item modified using the withItem1 method. Note that tuples are immutable, so a new object is returned. ```dart const t1 = Tuple2('a', 10); final t2 = t1.withItem1('c'); // t2 is a new [Tuple2] object with item1 is 'c' and item2 is 10. ``` -------------------------------- ### Tuple Equality and Collection Keys Source: https://context7.com/google/tuple.dart/llms.txt Tuples override `==` and `hashCode` for structural equality, making them suitable for `Map` keys and `Set` elements. Two tuples are equal if their items are identical and in the same order. ```dart import 'package:tuple/tuple.dart'; void main() { const k1 = Tuple3('admin', 1, true); const k2 = Tuple3('admin', 1, true); const k3 = Tuple3('user', 2, false); // Equality print(k1 == k2); // true print(k1 == k3); // false print(k1.hashCode == k2.hashCode); // true // As Map key final permissions = , List>{}; permissions[k1] = ['read', 'write', 'delete']; permissions[k3] = ['read']; // Lookup with a new but equal tuple print(permissions[Tuple3('admin', 1, true)]); // [read, write, delete] // As Set element final seen = >{}; seen.add(const Tuple2('a', 1)); seen.add(const Tuple2('a', 1)); // duplicate — ignored seen.add(const Tuple2('b', 2)); print(seen.length); // 2 } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.