Null Safety Exercise

Null Safety Exercise

Practice these exercises to master dart null safety. To practice these exercises, click on Run Online button and solve the problem.

Exercise 1: Null Safety In Dart

In variable name age, assign a null value to it using ?.

// Try to assign a null value to age variable using ?
void main() {
  int age;
  age = null;
  print("Age is $age");
}
Run Online

Exercise 2: Nullable Type Parameter For Generics

Try using ? to make the type parameter of List nullable.

// Try to make the type parameter of List nullable
void main() {
  List<int> items = [1, 2, null, 4];
  print(items);
}
Run Online

Exercise 3: Null Assertion Operator (!)

Try using null assertion operator ! to print null if the variable is null.

// Try to use null assertion operator(!) to print null if the variable is null
void main() {
  String? name;
  name = null;
  String name1 = name;
  print(name1);
}
Run Online

Exercise 4: Null Assertion Operator (!) For Generics

Try using null assertion operator ! to print null if the variable is null.

// Try to use null assertion operator(!) to print null if the variable is null
void main() {
  List<int?> items = [1, 2, null, 4];
 
  int firstItem = items.first;
  
  print(firstItem);
}
Run Online

Exercise 5: Null Assertion Operator (!) For Generics

Try using null assertion operator ! to print null if the variable is null.

// Try to use null assertion operator(!) to print null if the variable is null
int? returnNullButSometimesNot() {
  return -5;
}

void main() {
 int result = returnNullButSometimesNot().abs();
 print(result);
}
Run Online

**Exercise 6: Null Assertion Operator (!) **

Try using null assertion operator ! to print the length of the String or return null if the variable is null.

// Try to use null assertion operator(!) to print the length of the String or return null if the variable is null
int findLength(String? name) {
    // add null assertion operator here
  return name.length;
}

void main() {
  int? length = findLength("Hello");
  print("The length of the string is $length");
}
Run Online

Exercise 7: Null Coalescing Operator (??)

If you want to assign a default value to a variable if it is null, you can use null coalescing operator ??.

Try using null coalescing operator ?? to assign a default value to Stranger if it is null.

// Try to use null coalescing operator(??) to assign a default value to Stranger if it is null
void main() {
  String? name;
  name = null;
  String name1 = name;
  print(name1);
}
Run Online

Exercise 8: Type Promotion

Solve the error using type promotion:

// Try to solve the error using type promotion
Object name = "Mark";
print("The length of name is ${name.length}");
Run Online

Exercise 9: Type Promotion

Solve the error using type promotion:

// Try to solve the error using type promotion
import 'dart:math';
class DataProvider{
    String? get stringorNull => Random().nextBool() ? "Hello" : null;

    void myMethod(){
        if(stringorNull is String){
            print("The length of value is ${stringorNull.length}");
        }else{
            print("The value is not string.");
        }

    }
}

void main() {
    DataProvider().myMethod();
}
Run Online

Exercise 10: Late Keyword

Try using late keyword to solve the error:

// Try to solve the error using late keyword
class Person{
    String _name;

    void setName(String name){
        _name = name;
    }

    String get name => _name;
}

void main() {
    Person person = Person();
    person.setName("Mark");
    print(person.name);
}
Run Online