Menu
Coddy logo textTech

Intro to Null Safety

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

One of the most common sources of bugs in programming is attempting to use a value that doesn't exist. In Dart, this special "absence of value" is represented by null. Dart's null safety feature helps you catch these errors before your code even runs.

Without null safety, any variable could potentially be null, leading to runtime crashes when you try to use it:

// In older Dart (without null safety)
String name;  // Could be null!
print(name.length);  // Crash if name is null

With null safety enabled (Dart 2.12+), the compiler distinguishes between variables that can hold null and those that cannot. By default, variables are non-nullable - they must always contain a valid value:

String name = 'Alice';  // Must have a value
print(name.length);     // Safe - name is never null

The compiler will refuse to compile code where a non-nullable variable might be null. This shifts error detection from runtime to compile time, making your programs more reliable. In the upcoming lessons, you'll learn how to declare nullable types when you genuinely need them, and the operators Dart provides for working with potentially null values safely.

challenge icon

Challenge

Easy

Let's build a simple contact card system that demonstrates the importance of non-nullable variables. You'll create a system where every contact must have valid, guaranteed information.

You'll organize your code into two files:

  • contact.dart: Define a Contact class that represents a person's contact information. Since we're working with null safety, every field must have a valid value - no missing data allowed! Your class should have:
    • A String firstName for the contact's first name
    • A String lastName for the contact's last name
    • A String email for the contact's email address
    • An int phoneDigits for the number of digits in their phone number
    • A constructor that takes all four values
    • A getter fullName that returns the first and last name combined with a space between them
    • A displayCard() method that prints the contact's information
  • main.dart: Import your contact class and create two contacts to demonstrate that all fields are guaranteed to have values:
    • Create a contact with first name 'Emma', last name 'Wilson', email 'emma@mail.com', and phone digits 10
    • Create a contact with first name 'James', last name 'Brown', email 'james@work.com', and phone digits 11
    • Call displayCard() on each contact in the order listed above

The displayCard() method should print in this exact format:

--- Contact Card ---
Name: [fullName]
Email: [email]
Phone digits: [phoneDigits]

Notice how every variable must be initialized with a real value - this is null safety in action! The compiler guarantees that when you access fullName, email, or any other field, it will always contain valid data.

Expected output:

--- Contact Card ---
Name: Emma Wilson
Email: emma@mail.com
Phone digits: 10
--- Contact Card ---
Name: James Brown
Email: james@work.com
Phone digits: 11

Cheat sheet

Dart uses null safety to prevent bugs caused by using values that don't exist. The special value null represents the absence of a value.

With null safety enabled (Dart 2.12+), variables are non-nullable by default - they must always contain a valid value:

String name = 'Alice';  // Must have a value
print(name.length);     // Safe - name is never null

The compiler prevents code from compiling if a non-nullable variable might be null, shifting error detection from runtime to compile time.

Try it yourself

import 'contact.dart';

void main() {
  // TODO: Create first contact with:
  // - first name: 'Emma'
  // - last name: 'Wilson'
  // - email: 'emma@mail.com'
  // - phone digits: 10

  // TODO: Create second contact with:
  // - first name: 'James'
  // - last name: 'Brown'
  // - email: 'james@work.com'
  // - phone digits: 11

  // TODO: Call displayCard() on each contact in order
}
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