Discover how null safety can save your app from unexpected crashes and headaches!
Why Null safety in Flutter? - Purpose & Use Cases
Imagine you are building a mobile app where users can enter their profile information. Sometimes, users skip filling certain fields, leaving them empty. Without a way to handle these empty or missing values, your app might crash unexpectedly when trying to use that missing data.
Manually checking every piece of data for emptiness or absence slows down development and makes the code messy. It's easy to forget a check, causing bugs and crashes that frustrate users and waste your time fixing them.
Null safety helps by making the app aware of which values can be missing and which cannot. It forces you to handle missing data clearly and safely, preventing crashes before they happen and making your code cleaner and more reliable.
String name; print(name.length); // crashes if name is null
String? name; print(name?.length ?? 0); // safely handles null
With null safety, you can build apps that are more stable and trustworthy, giving users a smooth experience without unexpected crashes.
Think of a messaging app where some contacts don't have profile pictures. Null safety ensures the app won't crash trying to show a missing picture, instead it can show a default icon gracefully.
Null safety prevents app crashes caused by missing or empty data.
It makes your code cleaner by forcing clear handling of nullable values.
Apps become more reliable and user-friendly with null safety.
Practice
? symbol mean when used after a type in Flutter null safety?Solution
Step 1: Understand the
The?usage in Flutter?after a type means the variable can be null, allowing it to hold either a value or null.Step 2: Compare with non-nullable types
Without?, variables must always have a non-null value, so?explicitly allows null.Final Answer:
It means the variable can hold a null value. -> Option CQuick Check:
?means nullable = A [OK]
? means nullable, can hold null [OK]- Thinking
?makes variable constant - Confusing
?with non-nullable - Assuming
?means read-only
Solution
Step 1: Recall correct nullable syntax
In Flutter, to declare a nullable type, place?immediately after the type name, likeint?.Step 2: Check each option
int? number; usesint? number;which is correct. int number?; places?after variable name, which is invalid. nullable int number; uses a non-existent keyword. int number = null; assigns null to a non-nullable type, causing error.Final Answer:
int? number; -> Option AQuick Check:
Nullable type syntax =int?[OK]
? right after type to make nullable [OK]- Placing
?after variable name - Using non-existent keywords like 'nullable'
- Assigning null to non-nullable without
?
int? a; int b = a ?? 5; print(b);
Solution
Step 1: Understand variable initialization
Variableais nullable and not initialized, so it is null.Step 2: Evaluate the null-coalescing operator
The expression??a ?? 5means ifais null, use 5 instead.Step 3: Assign and print value
Sinceais null,bgets 5, soprint(b)outputs 5.Final Answer:
5 -> Option BQuick Check:
??gives default if null = 5 [OK]
?? to provide default when null [OK]- Assuming uninitialized nullable is zero
- Thinking
??returns null - Expecting compilation error for nullable without init
String? name; print(name.length);
Solution
Step 1: Analyze nullable variable usage
Variablenameis nullable and not initialized, so it is null.Step 2: Accessing property on nullable
Accessingname.lengthwithout null check causes error becausenamemight be null.Step 3: Correct usage
Use null-aware operator likename?.lengthor check for null before accessing.Final Answer:
Error: Cannot access property on nullable without check -> Option AQuick Check:
Access nullable property without check = error [OK]
?. or check null before property access [OK]- Assuming nullable property access is safe
- Ignoring null checks
- Confusing syntax errors with runtime errors
List<int>? numbers;, which code safely sums all elements or returns 0 if the list is null?Solution
Step 1: Understand nullable list and safe access
Sincenumberscan be null, accessing methods directly risks error.Step 2: Analyze each option
int sum = numbers?.reduce((a, b) => a + b) ?? 0; uses?.to safely callreduceonly ifnumbersis not null, then?? 0to default to 0 if null. int sum = numbers.reduce((a, b) => a + b); callsreducedirectly, causing error if null. int sum = numbers!.reduce((a, b) => a + b); forces non-null with!, risky if null. int sum = numbers == null ? null : numbers.reduce((a, b) => a + b); returns null instead of int 0, causing type mismatch.Step 3: Choose safe and correct option
int sum = numbers?.reduce((a, b) => a + b) ?? 0; safely sums or returns 0 if null.Final Answer:
int sum = numbers?.reduce((a, b) => a + b) ?? 0; -> Option DQuick Check:
Safe call + default value = int sum = numbers?.reduce((a, b) => a + b) ?? 0; [OK]
?. with ?? for safe nullable operations [OK]- Calling methods on nullable without safe operator
- Using
!without null check - Returning null instead of default value
Ready to prove it?
Pass the test and this pattern moves unknown
