Web Analytics Made Easy -
dart patterns

They are like special tools that help you do two main things: check if some data looks a certain way and easily take it apart to use its pieces.

Matching Patterns :

Imagine you have a box and you want to know if it contains a specific thing. Patterns help you do that. They let you check if the box holds something with a particular look or if it’s equal to something you know.

void main() {
var box = '';
// Our box
// The pattern we're looking for
var pattern =
if (box == pattern) {
// If the box matches the pattern…
print('We found the
} else {
inside the!');
// If it doesn't match…

Destructuring Patterns :

After you know that the box has what you’re looking for, patterns make it easy to open the box and take out its contents. It’s like magically splitting the box into smaller boxes, each holding one part of what’s inside.

void main() {
var box = ['', '0', '0']; // Our box with three items
// We open the box and get the parts.
var [item1, item2, item3] = box;
print('First item: $item1');
print('Second item: $item2'); print('Third item: $item3'); }

Identifier Pattern:

‘_’ can also be used as an identifier pattern in certain contexts. It’s helpful when you want to check the type of a value but don’t need to give it a name.

void main() {
var fruits = [42, 'apple'];
switch (fruits) {
case (int String _):
print('The first item is an integer, and the second item is a string!');
print('These items don\'t match our pattern. ');

Places patterns can appear

1. Variable Declaration:

Use patterns to declare variables and break down values in one go! Perfect for matching and deconstructing.

void main() {
var (a, [b, c]) =
('str', [1, 2]);
print('a: $a, b: $b, c: $c'); //a: str, b: 1, c: 2 }

This code declares variables a, b, and c using patterns and destructures values from the right-hand side into them.

2. Variable Assignment:

Swap values without extra variables!

void main() {
var (a, b) = ('left', 'right');
(b, a)
= (a, b); // Swap.
print('$a $b'); // "right left".

This code uses patterns to assign and swap values between variables a and b without using a temporary variable.

3. Destructuring Multiple Returns

Patterns are your secret code for extracting multiple values from a single function call. Imagine a function that returns a point’s coordinates:

var getPointCoordinates = 0 {
return {'x': 10, 'y': 20};
void main() {
var {'x': x, 'y': y} =
print('x: $x, y: $y'); //x: 10, y: 20

In this example, the getPointCoordinates function returns a Map with ‘x’ and ‘y’ coordinates. Patterns are used to destructure these values into x and y variables.

Click to rate this post!
[Total: 0 Average: 0]

By AmmarJaved

Ammar Javed possesses a unique combination of skills and expertise in web, desktop, and mobile app development, and content writing. Ammar Javed is a strategic thinker and has problem-solving abilities. Ammar Javed can work independently and in a team. Ammar Javed is dedicated to continuous learning and professional development.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »