Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Destructure a record inside a MapEntry in Dart Flutter

I have the following code:

  const mapEntry = MapEntry(('John', 'Doe'), 24);
  final MapEntry(key: name, value: age) = mapEntry;
  final (firstName, lastName) = name;
  print('first name: $firstName');
  print('last name: $lastName');
  print('age: $age');

The output is:

first name: John
last name: Doe
age: 24

Is it possible to destructure the ('John', 'Doe') record to firstName and lastName at the same line of destructuring the mapEntry?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Yep, it’s as simple as

  final MapEntry(key: (firstName, lastName), value: age) = mapEntry;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading