helpoatmeal
helpoatmeal

Reputation: 45

How can these dart functions be changed to work with null safety?

I'm new to coding and having trouble figuring out how to fix the errors having to do with adaptation to null safety.

https://dartpad.dartlang.org/?id=4bf7549c820d1adb4be8673e92820e43


void main() {
  
 int step1Result = add(n1: 5, n2: 9);
  
 int step2Result = multiply(step1Result, 5);
  
 double finalResult = step2Result / 3;
  
 print(finalResult);
  
}



int add({int n1, int n2}) {
  
  return = n1 + n2;
  
}

int multiply(int n1, int n2) {
  
  return = n1 * n2;
  
}

Upvotes: 1

Views: 40

Answers (1)

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63594

named constructor parameters are optional. You need to make them required or nullable.

int add({required int n1, required int n2}) {
  return n1 + n2;
}

int multiply(int n1, int n2) {
  return n1 * n2;
}

And nullable case, I am providing default value as 0

int add({int? n1, int? n2}) {
  return n1 ?? 0 + (n2 ?? 0);
}

You can explore more null-safety And using-constructors

Upvotes: 4

Related Questions