Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The is operator is a runtime type test operator that evaluates whether an object is an instance of a specified type or its subtype. It returns a boolean true if the object’s runtime type matches the specified type hierarchy, and false otherwise. Dart also provides the is! operator, which is the exact logical negation of is, returning true if the object is not an instance of the specified type.

Syntax

expression is Type
expression is! Type

Core Mechanics

1. Subtype Resolution The is operator respects Dart’s object-oriented inheritance and interface implementations. If a class Child extends or implements Parent, evaluating a Child instance against the Parent type yields true.
class Parent {}
class Child extends Parent {}

Parent obj = Child();
bool result1 = obj is Child;  // true
bool result2 = obj is Parent; // true
bool result3 = obj is Object; // true
2. Type Promotion via Flow Analysis When the is operator is used in a conditional statement (like if or switch), the Dart analyzer performs control flow analysis. If the test evaluates to true, the compiler automatically promotes the variable from its declared type to the tested type within the scope of that execution branch. This promotion only applies to local variables and parameters, not to instance variables or getters, as their values could change asynchronously.
Object data = "Dart";

if (data is String) {
  // Within this block, 'data' is statically promoted to type 'String'.
  // String-specific members are now accessible without explicit casting.
  int len = data.length; 
}
3. Nullability Handling The is operator strictly evaluates against Dart’s sound null safety system. Testing a null value against a non-nullable type evaluates to false. Testing a null value against a nullable type (denoted by ?) evaluates to true.
int? nullableValue = null;

bool test1 = nullableValue is int;  // false
bool test2 = nullableValue is int?; // true
4. Generic Type Reification Because Dart uses reified generics, the is operator can evaluate the specific type arguments of generic collections at runtime, unlike languages that use type erasure.
Object list = <int>[1, 2, 3];

bool isIntList = list is List<int>;       // true
bool isStringList = list is List<String>; // false
Master Dart with Deep Grasping Methodology!Learn More