Now Wildcard variables feature is available in Dart 3.7
This great feature, also introduced in dot after 3.7, is called wildcard variables. What are wildcard variables? This is "_." We can utilize them by using underscore, which is a way to ignore any return value inside our code. The following are a few use cases where that variable will be very handy. Let's start discussing those one by one. Before jumping into the post details, if you are interested in the demonstration video, check this out on my YouTube channel card, whats new in dart 3 7 - Dart features.
Ignore value in tuple
Let's first discuss what a tuple is. When we group two or more variables without creating a specific class or strict, then this object is called a tuple. So how we can do that we can in Dart, we can do that by placing those inside round brackets. Let's say we have two values and we want to create a tuple of them, so we can do that like the following.
var (firstValue, secondValue) = ('Usman', 'Code');
Here, firstValue will haveUsman
, and secondValue will have Code
value in it. Let's now jump back to its definition. we have a record or tuple, and we want to ignore, let's say, lastName.
var (firstName, middlename, lastName) = ('Usman', 'Channel', 'Code');
We can put simply underscore at the third position while creating variables so usman will be mapped to the first name channel to the middle name and code will be ignored, and if now I run the code, it will print channel name Usman channel
.
// Output: Name: Usman Code
print('Channel Name: $firstName $middlename');
Ignore coordinates in pair
The second use case almost aligns with the first one, but this is related to the coordinate pointer. When we want to ignore, let's say, the x
or y
coordinate, specifically when we are developing something which is to do with the coordinates, we can easily utilize that underscore.
var (x, _) = (10, 20);
// Output: Y coordinate is 20
print('X coordinate is $x');
so in this example we are ignoring second value we are mapping first value with the y which is not right logically so first should be and always be x so now the first value will be mapped with x the second value will be ignored so we can simply print x out of that one so let me save and run the value should say X coordinate is 10
.
Wildcard in switch statement
The third use case is wildcard in a switch statement
. This is to do with the scenario when we have an object, and we only want to use a specific property of that in the case, and we want to ignore the others.
const input = (200, 'success');
switch (input) {
case (_, 'success'):
print('Operation was successful.');
break;
case (_, 'error'):
print('Something went wrong.');
break;
default:
print('Unknown status.');
}
As demonstrated in the above code, let's say we have that input coming from the top which has first property 200 second string which says success and let's say this is response coming back from an API of status 200 and we want to check if this is a case of status 200 we can simply say that operation was successful.
Ignore parts in if else
We have an object, and we have an if, and inside that,t we only want to check a property of it to compare with a value. Let's say the second one we want to check and we want to say if that is 200, then everything is fine.
var result = ('ok', 100);
if (result case (_, 200)) {
print('Everything is fine');
} else {
print("Its not okay");
}
If its not 200, then else part will be executed and should print, "Its not okay".
Loop destructuring
For this, let's assume we have a list of data, three objects.
List<(int, String, int)> data = [
(1, 'Usman', 20),
(2, 'Ali', 21),
(3, 'Ahmed', 23),
];
In above code, we want to ignore the ids let's say we want to show that list on the UI in a list view and we don't want to display those numbers or indexes, so for that, we can simply use underscore within our for loop and we can only utilize name and age.
for (var (_, name, age) in data) {
print('User: $name and the age is $age');
}
If I run the above code, it should say name is Usman and age is 20. The same pattern would be displayed for Ali and Ahmed.
Named record destructuring
Let's jump to the last case, which is named record destructuring
and now let's go to its definition, let's say we have a record and we want to ignore a value same thing we did for the case of for loop but this is specifically for a single record we want to ignore the age from that record and we only want to print its name and country.
var record = (name: 'Usman', age: 30, country: 'Pakistan');
var (name: userName, age: _, country: userCountry) = record;
print('User: $userName from $userCountry');
So in that case, I will run now and it would say "user: Usman from Pakistan".
Those were the different use cases and this is how much useful the wildcard is and it will cover a lot of scenarios and remove a lot of boilerplate code from within our core bases now onward. For the bonus points, I have shared in my youtube video, you can have them in last two minutes, happy coding.