Search⌘ K
AI Features

Verifying Data Types at Runtime

Explore how to verify data types dynamically in Dart using type test operators such as is, is!, and as. Understand the syntax, risks of incorrect casting, and how to safely check types within conditional statements to write more reliable and maintainable code.

Type test operators evaluate the type of an object at runtime. We use these operators to verify what kind of data a variable holds before performing type-specific operations.

Below is a list of the type test operators supported by Dart.

Operator

Use

as

Typecasts an object to a specific type

is

Evaluates to

true

if the object has the specified type

is!

Evaluates to

true

if the object does not have the specified type

The as operator performs a typecast. We should only use as when we are absolutely certain of an object's type, typically after performing a prior is check. Casting incorrectly will cause our program to crash with a runtime error.

Syntax

Type test operators require two operands, and the order is strictly enforced. The value we are checking must sit on the left side of the operator, and ...