Dart Null Safety

A null value means "nothing here". Calling a method on nothing crashes a program, and older languages suffered from this problem for decades. Dart solves it with null safety: the compiler checks your code before the app runs and rejects any line that might touch an empty value.

The Empty Box Idea

Imagine a gift box. A normal box always holds a gift. A special box may hold a gift or may sit empty. Dart makes you label special boxes with a question mark so everyone knows to check before opening.


  String name     ┌───────────┐
                  │  "Asha"   │   always holds text
                  └───────────┘

  String? nick    ┌───────────┐
                  │  null     │   may hold text or nothing
                  └───────────┘

Non-Nullable by Default

Every variable in Dart refuses null unless you say otherwise.


String city = 'Pune';
city = null;   // ERROR: null is not allowed

The compiler flags the second line immediately, long before a user sees a crash.

Nullable Types

Add ? after the type to allow null.


String? nickname;       // starts as null
int? age = 25;
age = null;             // allowed

Safe Ways to Use Nullable Values

Check with if


String? nickname = 'Ria';
if (nickname != null) {
  print(nickname.length);   // safe inside the check
}

Dart remembers the check and treats the variable as non-null inside the block.

Null-Aware Access: ?.

The ?. operator calls a method only when the value exists. Otherwise the whole expression returns null.


String? nickname;
print(nickname?.toUpperCase());   // prints null, no crash

Default Values: ??

The ?? operator supplies a fallback when the left side is null.


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

Assign If Null: ??=


String? theme;
theme ??= 'light';   // sets theme only when it is null

The Bang Operator: !

The ! operator tells Dart "trust me, this is not null".


String? title = 'Flutter';
print(title!.length);

Dart throws an error at runtime if the value is null. Use ! only when logic guarantees a value exists. Prefer ?. and ?? in most situations.

Operator Summary

SymbolNameMeaning
String?Nullable typeValue may be null
?.Null-aware accessSkip the call when null
??If-nullUse a fallback value
??=Assign if nullFill an empty variable
!Null assertionInsist the value exists

The late Keyword

Some variables receive a value after declaration but before first use. The late keyword promises Dart that the value arrives in time.


late String userName;

void login() {
  userName = 'Kabir';
}

Reading a late variable before assignment throws an error, so assign it early.

Required Named Parameters

Flutter widgets use named parameters heavily. The required keyword forces the caller to supply a value.


class Product {
  final String name;
  final double price;

  Product({required this.name, required this.price});
}

A call without name fails to compile. Optional parameters use a nullable type or a default value instead.

Null Safety in Flutter Code

Widgets such as Text reject a null string. A user profile that may lack a name needs a fallback:


Text(user.name ?? 'Anonymous')

Writing code this way removes the red crash screen from your users' phones.

Why Null Safety Helps Beginners

Before null safety, a missing value stayed invisible until a user triggered it. The app crashed with an error message that pointed far from the real cause. Dart now moves that discovery to your editor. A red underline appears while you type, and the message explains exactly which variable might be empty.

New learners often feel annoyed by these warnings. Treat each warning as a free code review. The compiler spots problems that would cost hours of testing to find by hand.

Practice Ideas

Write a function that receives a nullable email address and returns the part before the at sign, or the word "unknown" when the email is missing. Build a small class with one required name and one optional nickname. Print both values using the ?? operator to supply defaults. These small exercises build the habit of thinking about empty values before they cause trouble.