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 Null-aware Operators


Share

Dart Null-aware Operators

Null-aware operators are used with nullable values, values that can be null.

They let you handle null in a short and safe way, without an if statement.

OperatorNameDescription
a ?? bIf-nullGives a, or b if a is null
a ??= bNull-aware assignmentGives a the value b, only if a is null
a?.bNull-aware accessReads b, or gives null if a is null
a?..b()Null-aware cascadeRuns the cascade, only if a is not null
a?[i]Null-aware indexReads item i, or gives null if a is null

If you are new to null, read the Null Safety chapter first.

In some examples on this page, the variables are declared outside main(), as top-level variables.

Inside main(), Dart would see that the value is fixed and warn that the null-aware operator is not needed.


The ?? Operator

The ?? operator gives a default value when the left side is null:

Example

void main() {
  int? score = int.tryParse('abc');
  int? level = int.tryParse('3');

  print(score ?? 0);
  print(level ?? 1);
}
Try it Yourself »

Result:

0
3

score is null, so we get the default value 0.

level has a value, so we get that value.



The ??= Operator

The ??= operator gives a variable a value, but only if the variable is null:

Example

void main() {
  int? width = int.tryParse('abc');
  int? height = int.tryParse('40');

  width ??= 100;
  height ??= 100;

  print(width);
  print(height);
}
Try it Yourself »

Result:

100
40

The ?. Operator

Use ?. instead of . to read a property or call a method on a nullable value.

If the value is null, the result is null, and the program does not stop:

Example

String? city = 'Paris';
String? country;

void main() {
  print(city?.toUpperCase());
  print(country?.toUpperCase());
}
Try it Yourself »

Result:

PARIS
null

You can combine ?. and ?? to get a default value:

Example

String? nickname;

void main() {
  int length = nickname?.length ?? 0;
  print(length);
}
Try it Yourself »

Result:

0

The ?[] Operator

Use ?[] to read an item from a list that can be null.

A list is a group of values in square brackets (you will learn more in the Lists chapter):

Example

List<String>? fruits = ['Apple', 'Banana'];
List<String>? colors;

void main() {
  print(fruits?[0]);
  print(colors?[0]);
}
Try it Yourself »

Result:

Apple
null

The ?.. Operator

The ?.. operator is the null-aware version of the cascade operator ...

A cascade calls several methods on the same object, one after another.

A StringBuffer is a built-in object that builds a string piece by piece, and write() adds text to it.

The ?.. operator runs the cascade only if the value is not null:

Example

StringBuffer? buffer = StringBuffer();
StringBuffer? empty;

void main() {
  buffer
    ?..write('Hello')
    ..write(' World');
  print(buffer);

  empty
    ?..write('Hello')
    ..write(' World');
  print(empty);
}
Try it Yourself »

Result:

Hello World
null

You can read more about cascades in the Cascade Notation 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.