Records & Patterns — Dart | Flutter 💙
Unlock the Full Potential of Your Dart Applications with Powerful Records and Patterns 🥷🏻
From Dart 3ɑ, the Flutter team introduced a new data type called Records & Patterns. Do you know what they are? Not to worry. Let’s check them out in detail with the help of this article 🥳
NOTE: When this article was written, Records & Patterns were available in Flutter Master Channel.
Records allow you to efficiently and concisely create anonymous composite values from any existing data. In other words, you can return multiple values from a function without making a model/temporary class for it!!!
What are Patterns or Pattern Matching? Patterns can destructure composite data into its constituents. Patterns are fully type-safe and checked during development. Well, Patterns allow us to conditionally assign parameters simply using if-else.
Initially, we created a model class to store multiple values, like the following:
And then return them from our function. Something like the following:
Easy and simple right? But now let’s check out how it will look when we use Records 🤩
So, using Records & Patterns is quite simple. You can return
lastName together from our function as follows:
Records & Patterns make our life simpler. We don’t have to create model classes with 2–3 variables. We can simply use Records & Patterns for this purpose 🥳
Don’t stop, until you are breathing!💙
- Abhishek Doshi