Menu
Coddy logo textTech

Null-Aware Operators

Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 27 of 110.

Beyond ?. and !, Dart provides additional operators that make working with nullable values more concise. These null-aware operators help you write cleaner code when dealing with potential null values.

The null-coalescing operator ?? returns the left value if it's not null, otherwise it returns the right value:

String? nickname;
String displayName = nickname ?? 'Guest';
print(displayName);  // Guest

nickname = 'Alex';
displayName = nickname ?? 'Guest';
print(displayName);  // Alex

The null-aware assignment operator ??= assigns a value only if the variable is currently null:

String? username;
username ??= 'Anonymous';
print(username);  // Anonymous

username ??= 'NewName';
print(username);  // Still Anonymous (wasn't null)

You can chain the null-aware access operator ?. for nested properties:

String? city = user?.address?.city;  // Safe even if user or address is null

These operators combine well together. For example, you might access a nullable property and provide a default:

String greeting = user?.name ?? 'Stranger';
// If user is null OR user.name is null, use 'Stranger'

Using these operators keeps your code readable while maintaining null safety throughout your program.

challenge icon

Challenge

Easy

Let's build a guest registration system that demonstrates how null-aware operators can make your code cleaner when handling optional information.

You'll organize your code into two files:

  • guest.dart: Define a Guest class that represents someone attending an event. Some guests provide full details, while others only give the basics. Your class should have:
    • A non-nullable String name - every guest must have a name
    • A nullable String? email - not everyone provides an email
    • A nullable String? dietaryPreference - some guests don't specify dietary needs
    • A constructor that takes name as a required parameter
    • A method getDisplayEmail() that returns the email if available, or 'No email provided' using the ?? operator
    • A method assignDefaultDiet() that sets dietaryPreference to 'Standard' only if it's currently null, using the ??= operator
    • A method getEmailDomain() that returns the length of the email using ?., or null if no email exists
    • A method printBadge() that prints the guest's badge information
  • main.dart: Import your guest class and register three guests with varying levels of information:
    • Create a guest named 'Alice', set her email to 'alice@company.com' and dietary preference to 'Vegetarian'
    • Create a guest named 'Bob', set only his email to 'bob@email.org' (leave dietary preference as null)
    • Create a guest named 'Charlie' (leave both email and dietary preference as null)
    • Call assignDefaultDiet() on all three guests
    • For each guest in order, call printBadge(), then print Email length: [result] using getEmailDomain()

The printBadge() method should print in this format:

=== Guest Badge ===
Name: [name]
Email: [getDisplayEmail()]
Diet: [dietaryPreference or "Not specified"]

Notice how Alice's dietary preference stays 'Vegetarian' because ??= only assigns when the value is null, while Bob and Charlie get the default 'Standard' diet assigned.

Expected output:

=== Guest Badge ===
Name: Alice
Email: alice@company.com
Diet: Vegetarian
Email length: 17
=== Guest Badge ===
Name: Bob
Email: bob@email.org
Diet: Standard
Email length: 13
=== Guest Badge ===
Name: Charlie
Email: No email provided
Diet: Standard
Email length: null

Cheat sheet

Dart provides null-aware operators to work with nullable values more concisely.

The null-coalescing operator ?? returns the left value if it's not null, otherwise returns the right value:

String? nickname;
String displayName = nickname ?? 'Guest';
print(displayName);  // Guest

nickname = 'Alex';
displayName = nickname ?? 'Guest';
print(displayName);  // Alex

The null-aware assignment operator ??= assigns a value only if the variable is currently null:

String? username;
username ??= 'Anonymous';
print(username);  // Anonymous

username ??= 'NewName';
print(username);  // Still Anonymous (wasn't null)

You can chain the null-aware access operator ?. for nested properties:

String? city = user?.address?.city;  // Safe even if user or address is null

These operators can be combined together:

String greeting = user?.name ?? 'Stranger';
// If user is null OR user.name is null, use 'Stranger'

Try it yourself

import 'guest.dart';

void main() {
  // TODO: Create a guest named 'Alice'
  // Set her email to 'alice@company.com'
  // Set her dietary preference to 'Vegetarian'

  // TODO: Create a guest named 'Bob'
  // Set only his email to 'bob@email.org'
  // Leave dietary preference as null

  // TODO: Create a guest named 'Charlie'
  // Leave both email and dietary preference as null

  // TODO: Call assignDefaultDiet() on all three guests

  // TODO: For each guest in order (Alice, Bob, Charlie):
  // - Call printBadge()
  // - Print 'Email length: [result]' using getEmailDomain()
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming