Google News
logo
Dart - Interview Questions
How do you handle null values in Dart?
In Dart, null values can be handled using various techniques and operators to ensure code safety and prevent null-related errors. Here are some approaches to handle null values in Dart:

1. Null-aware Operators :

   * Conditional Access Operator (?.) : This operator allows you to access properties or call methods on an object only if the object is not null. If the object is null, the expression returns null without throwing an exception. For example:
     String? name;
     int nameLength = name?.length ?? 0; // Using ?. and ?? operators to handle null​

   * Null-aware Assignment Operator (??=) : This operator assigns a value to a variable only if the variable is currently null. If the variable is already assigned a non-null value, the assignment is skipped. For example:
     String? name;
     name ??= 'John'; // Assigning a default value if name is null​


2. Null Check Operators :

   * Null-aware Access (?.) : The null-aware access operator (?.) is used to safely access properties or methods on an object. If the object is null, the entire expression evaluates to null. For example:
     String? name;
     if (name?.isEmpty == true) {
       // Code block executes only if name is not null and is empty
     }​

   * Null Assertion Operator (!) : The null assertion operator (!) asserts that an expression is not null, allowing you to override null safety checks. It should be used with caution as it may lead to a runtime exception if the expression is null. For example:
     String? name;
     int nameLength = name!.length; // Asserting that name is not null​
3. Conditional Statements :

   * if-null Operator (??) : This operator provides a concise way to specify a default value when a variable is null. If the variable is null, the expression after ?? is evaluated and returned. For example:
     String? name;
     String fullName = name ?? 'Guest'; // Assigning a default value if name is null​

   * Conditional Statements : Traditional conditional statements, such as `if` and `else`, can be used to explicitly handle null values. You can check if a variable is null and take appropriate actions based on that. For example:
     String? name;
     if (name == null) {
       // Code block executes if name is null
     } else {
       // Code block executes if name is not null
     }​


4. Null Safety :

   Dart 2.12 introduced null safety, which provides compile-time checks to help prevent null-related errors. By enabling null safety, variables are classified into nullable (`Type?`) and non-nullable (`Type`) types. Non-nullable types guarantee that a value is never null, while nullable types allow null values. This helps in catching potential null-related issues during compilation.

   For example :
   String? nullableName = null;
   String nonNullableName = 'John';​

   Null safety encourages the use of non-nullable types whenever possible, reducing the chances of encountering null-related errors.
Advertisement