Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Dart Tutorial

Dart HOME Dart Intro Dart Get Started Dart Syntax Dart Output Dart Comments Dart Variables Dart final and const Dart Data Types Dart Numbers Dart Strings Dart String Interpolation Dart String Methods Dart Booleans Dart Type Conversion Dart Null Safety Dart late Dart Operators Dart Arithmetic Dart Assignment Dart Comparison Dart Logical Dart Type Test Dart Null Operators Dart Cascade Dart If...Else Dart Switch Dart While Loop Dart For Loop Dart Break/Continue

Dart Collections

Dart Lists Dart Access List Items Dart Change List Items Dart Loop Lists Dart List Methods Dart Sets Dart Maps Dart Access Map Items Dart Loop Maps Dart Collection if/for Dart Spread Operator

Dart Functions

Dart Functions Dart Parameters Dart Named Parameters Dart Return Values Dart Arrow Functions Dart Anonymous Functions Dart Closures Dart Scope Dart Recursion

Dart Classes

Dart OOP Dart Classes/Objects Dart Constructors Dart Named Constructors Dart Factory Constructors Dart Getters and Setters Dart Static Members Dart Inheritance Dart Abstract Classes Dart Interfaces Dart Mixins Dart Extension Methods Dart Enums Dart Generics

Dart 3 Features

Dart Records Dart Patterns Dart Switch Expressions Dart Sealed Classes Dart Class Modifiers

Dart Errors

Dart Exceptions Dart Custom Exceptions

Dart Async

Dart Async Intro Dart Futures Dart async/await Dart Streams Dart Isolates

Dart Libraries

Dart Imports Dart Packages Dart Math Dart Dates Dart JSON Dart RegExp Dart User Input Dart Files Dart Testing

Dart Examples

Dart Examples Dart Exercises


Dart Type Test Operators


Share

Dart Type Test Operators

Type test operators are used to check the type of a value when the program runs.

OperatorDescriptionExample
isReturns true if the value has the typex is int
is!Returns true if the value does not have the typex is! int
asTreats the value as the type (a type cast)x as String

In the examples on this page, we use the type Object.

An Object variable can hold any value that is not null, like a number, a string or a bool.


The is Operator

The is operator returns true if a value has a type:

Example

void main() {
  Object value = 42;

  print(value is int);
  print(value is String);
  print(value is num);
}
Try it Yourself »

Result:

true
false
true

value is num is also true, because every int is also a num.



The is! Operator

The is! operator is the opposite of is.

It returns true if a value does not have a type:

Example

void main() {
  Object value = 'Hello';

  print(value is! int);
  print(value is! String);
}
Try it Yourself »

Result:

true
false

Type Promotion with is

When you check a type with is in an if statement, Dart remembers the result inside the block.

This is called type promotion, the same as with null checks.

In the example below, toUpperCase() only exists on strings, but we can use it because Dart knows that value is a String inside the block:

Example

void main() {
  Object value = 'dart';

  if (value is String) {
    print(value.toUpperCase());
    print(value.length);
  }
}
Try it Yourself »

Result:

DART
4

Without the check, the program does not run, because an Object has no toUpperCase() method:

Example

void main() {
  Object value = 'dart';
  print(value.toUpperCase());
}
Try it Yourself »

Result:

main.dart:3:15: Error: The method 'toUpperCase' isn't defined for the type 'Object'.
 - 'Object' is from 'dart:core'.
Try correcting the name to the name of an existing method, or defining a method named 'toUpperCase'.
  print(value.toUpperCase());
              ^^^^^^^^^^^

The as Operator

The as operator tells Dart to treat a value as a specific type.

This is called a type cast:

Example

void main() {
  Object value = 'dart';
  String text = value as String;
  print(text.toUpperCase());
}
Try it Yourself »

Result:

DART

If the value does not have that type, the program stops with an error when it runs:

Example

void main() {
  Object value = 42;
  String text = value as String;
  print(text);
}
Try it Yourself »

Result:

Unhandled exception:
type 'int' is not a subtype of type 'String' in type cast

Tip: An is check is safer than as, because it never stops the program.

Use as only when you are sure about the type.

The as operator does not convert values.

To turn the string '42' into a number, use int.parse() from the Type Conversion chapter.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.