Imagine you have a box of toys. Some are cars, some are dolls, and some are building blocks. If you wanted to play a game that only uses cars, you would first check each toy to see if it’s a car, right? In programming, we do the same thing with type matching.
Dart is a language that works with different types of data, like numbers, words (strings), and even custom objects. Sometimes, we need to check what type something is before we use it. That’s where type matching comes in! In this article, we will learn how to check types, match them in switch
statements, and handle mixed types safely.
Checking Types with is
Dart has a simple way to check if something is a certain type. It’s called the is
keyword. Think of it like asking, “Is this a car?” before playing a car game.
Example: Checking a Variable’s Type
void main() {
var item = "Hello, Dart!";
if (item is String) {
print("This is a string!");
} else {
print("This is not a string.");
}
}
What’s Happening Here?
- The variable
item
holds some text (a string). - We use
item is String
to check if it’s a string. - If it is, we print “This is a string!”.
- If it’s something else, we print “This is not a string.”
Try changing item
to a number and see what happens!
Type Matching with switch
and Patterns
Sometimes, we don’t just want to check if something is a certain type—we want to handle different types differently. This is where switch
and pattern matching come in!
Think of it like sorting laundry. Socks go in one pile, shirts in another, and pants somewhere else. In Dart, we can use switch
to separate different types.
Example: Handling Multiple Types
void main() {
dynamic value = 42; // Try changing this to a string or a double
switch (value) {
case int():
print("This is an integer.");
break;
case double():
print("This is a decimal number.");
break;
case String():
print("This is a string.");
break;
default:
print("This is something else.");
}
}
What’s Happening Here?
- We create a
value
that can be any type. - The
switch
statement checks what typevalue
is. - If it’s an
int
, we say “This is an integer.” - If it’s a
double
, we say “This is a decimal number.” - If it’s a
String
, we say “This is a string.” - If it’s something else, we print “This is something else.”
Try changing value
to different things and see how Dart sorts them!
Handling Nullable and Mixed Types
In Dart, a variable can sometimes be null, which means it has no value. We need to handle this carefully so our program doesn’t crash.
Think of it like checking if your backpack is empty before going to school. If it’s empty, you wouldn’t want to leave without your books!
Example: Handling Null Values
void main() {
String? name;
if (name is String) {
print("Hello, $name!");
} else {
print("Oops! No name was given.");
}
}
What’s Happening Here?
- We create a
name
variable that can be null. - We check if
name
is aString
. - If it is, we say hello.
- If not, we print “Oops! No name was given.”
Try assigning a name to name
and see what happens!
Conclusion
Now you know how to check and match types in Dart. You learned how to:
- Use
is
to check the type of a variable. - Use
switch
statements with pattern matching to handle different types. - Handle
null
values safely.
Understanding type checking is essential when working with dynamic data. Try building a program that processes user input and categorizes it based on its type. Experiment with different approaches to deepen your understanding and improve your coding skills.