Home > Blockchain >  Constructor Null safety in Dart
Constructor Null safety in Dart

Time:06-23

Tell me please. Why is pr2 null and not str1 by default?

void main() {
  var house = Home(pr1: 4, pr3: 5);
  house.printDate();
}

class Home {
  int pr1=1;
  String? pr2 = 'str1';
  int pr3=3;
  Home({required this.pr1, this.pr2, required this.pr3});

  void printDate() {
    print('The pr1: $pr1');
    print('The pr2: $pr2');
    print('The pr3: $pr3');
  }
}

The pr1: 4 The pr2: null The pr3: 5

CodePudding user response:

Because optional parameters are by default null unless otherwise specified in the constructor definition. If you want to declare another default value, you need to use this syntax:

void main() {
  var house = Home(pr1: 4, pr3: 5);
  house.printDate();
  // The pr1: 4
  // The pr2: str1
  // The pr3: 5
}

class Home {
  int pr1=1;
  String? pr2;
  int pr3=3;
  Home({required this.pr1, this.pr2 = 'str1', required this.pr3});

  void printDate() {
    print('The pr1: $pr1');
    print('The pr2: $pr2');
    print('The pr3: $pr3');
  }
}

CodePudding user response:

I refactored your code for readability and would drop the first solution using if-else statement

class Home {
  //initialize the variables using the final keyword
  final int? pr1;
  final String? pr2;
  final int? pr3;
  
  //set the constructors
  Home({required this.pr1, this.pr2, required this.pr3});
  
void printDate(){
  //using the pr1 value
  print('The pr1: $pr1');
  //perfoming a runtime assertation for dev
  //assert(pr2 == null);//if the value for pr2 is null; it run smoothly
  //using if-else statement, I can tell wht is shows as outcome
  if (pr2 == null){
    print('The pr2: str1');
  }else{print('The pr2: $pr2');}//I would be rewriting this as a tenary operator


  //using the pr3 value
  print('The pr3: $pr3');
}
}
//the app starts ro run from here  
void main(){
  var house = Home(pr1:5,pr3:6,);
  house.printDate();
}

From the above, you can see that when the condition for pr2 is null, it displays the text 'str1' and if it is not null, it displays the value of pr2. Hope this helps alot.

CodePudding user response:

The second way would be to use a tenary operator to check for null

class Home {
  //initialize the variables using the final keyword
  final int? pr1;
  final String? pr2;
  final int? pr3;

  //set the constructors
  Home({required this.pr1, this.pr2, required this.pr3});

  void printDate() {
    //using the pr1 value
    print('The pr1: $pr1');
//pr2 check for null safety using tenary
    pr2 == null ? print('The Pr2: str1'):print('The pr2:$pr2');
    //using the pr3 value
    print('The pr3: $pr3');
  }
}

//the app starts ro run from here
void main() {
  var house = Home(
    pr1: 5,
    pr3: 6,
  );
  house.printDate();
}

What does tenary mean? simply put, think of it as a logic condition, a form of if-else but in an easier way, the way to express this is:

 condition?expression1:expression2;

if the condition is true, expression 1 runs, if the condition is false, expression 2 runs. To answer your question, from dart 2.12, it became null safe, it is not a safe practice to variables to constructors. I would be glad to explain further if the need be.

  • Related