You are currently viewing Dart: Type Information

Dart: Type Information

Imagine you have a mystery box. You don’t know what’s inside until you open it! But wouldn’t it be nice if the box had a label telling you what’s inside before you even open it? In Dart, type information works like that label — it tells us what kind of data we are working with.

Dart is a statically typed language, which means it tries to keep track of the types of variables while you write your code. But sometimes, we need to check or confirm a variable’s type while the program is running. This is where type information becomes useful!

In this article, we’ll explore how Dart handles type information, how to check a variable’s type, and how to use runtimeType to inspect types dynamically.

Understanding Dart’s Type System

Dart has two ways of dealing with types:

  1. Static typing – This means Dart knows the type of a variable before the program runs.
  2. Dynamic typing – This means a variable’s type is determined while the program is running.

Let’s look at an example:

void main() {

  int age = 12; // Static typing: Dart knows 'age' is an integer
  print(age);

  var name = "Alice"; // Dart figures out 'name' is a string
  print(name);

  dynamic anything = 42; // Dynamic typing: This can change later
  print(anything);

  anything = "Dart";
  print(anything);

}

When to Use Explicit Types vs. var and dynamic

  • Use explicit types (int, String, bool, etc.) when you want clear, predictable variables.
  • Use var when Dart can figure out the type for you.
  • Use dynamic if you want a variable that can hold different types (but be careful — it can lead to errors!).

Getting Type Information with runtimeType

Dart has a built-in way to check what type a variable is while the program is running. It’s called runtimeType.

Think of runtimeType as a name tag that tells you exactly what kind of data you’re dealing with.

Example: Checking the Type of a Variable

void main() {

  var number = 100;
  var text = "Hello!";
  var floatingNumber = 3.14;

  print(number.runtimeType); // Prints: int
  print(text.runtimeType); // Prints: String
  print(floatingNumber.runtimeType); // Prints: double

}

When is runtimeType Useful?

  • When you’re debugging and need to check what type a variable is.
  • When you’re working with dynamic values and need to confirm their type.

Type Checking and Type Casting

Sometimes, we need to check if a variable is a certain type before using it. Dart gives us two important tools for this:

  • is → Checks if a variable is a specific type.
  • as → Converts (or casts) a variable to another type.

Example: Checking a Variable’s Type

void main() {

  var value = "Dart";

  if (value is String) {
    print("It's a string!");
  } else {
    print("It's not a string.");
  }

}

This is like checking if a key fits a lock before trying to open it!

Example: Type Casting with as

void main() {

  dynamic something = "Hello, world!";

  // Casting to a String
  String text = something as String;

  print(text.toUpperCase()); // HELLO, WORLD!

}

Be careful! If you try to cast something incorrectly (like turning a number into a string), Dart will throw an error. It would be wise to check the type before performing a cast.

Working with Generic Types

Dart also allows us to work with generic types, which means we can create flexible and safe data structures.

Imagine you have a toy box that only holds cars. If someone tries to put a doll inside, it won’t fit! Generics help us create lists, maps, and functions that only accept the right type of data.

Example: Using Generics in a List

void main() {

  List<int> numbers = [1, 2, 3, 4]; // Only integers are allowed

  numbers.add(5); // Works fine
  // numbers.add("hello"); // ERROR! This is not an int

  print(numbers);

}

By using <int>, we tell Dart that this list can only contain integers. This prevents mistakes before they even happen!

Conclusion

You now have a solid understanding of Dart’s type information. You’ve learned how to:

  • Differentiate between Dart’s static and dynamic type system.
  • Use runtimeType to check a variable’s type.
  • Apply is and as for type checking and casting.
  • Utilize generics to create safer lists and functions.

Mastering type information will help you write more reliable and efficient programs. Experiment with different types and see how they behave.

Leave a Reply