1

can i ask? I'm studying the language dart Functions with Optional Parameters. When I run this code an error occurs and I'm confused to fix it. can you help me solve this:

void gfg1(int g1, [ int g2 ]) 
{ 
  // Creating function 1 
  print("g1 is $g1"); 
  print("g2 is $g2"); 
} 

void gfg2(int g1, { int g2, int g3 }) 
{ 
  // Creating function 1 
  print("g1 is $g1"); 
  print("g2 is $g2"); 
  print("g3 is $g3"); 
} 

void gfg3(int g1, { int g2 : 12 }) 
{ 
  // Creating function 1 
  print("g1 is $g1"); 
  print("g2 is $g2"); 
} 

void main() 
{ 
  // Calling the function with optional parameter 
  print("Calling the function with optional parameter:"); 
  gfg1(01); 

  // Calling the function with Optional Named parameter 
  print("Calling the function with Optional Named parameter:"); 
  gfg2(01, g3 : 12); 

  // Calling function with default valued parameter 
  print("Calling function with default valued parameter"); 
  gfg3(01); 
 } 

problem :

The parameter 'g2' can't have a value of 'null' because of its type, but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.

The parameter 'g3' can't have a value of 'null' because of its type, but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.

this my dart version : Dart SDK version: 2.12.0-179.0.dev (dev) (Wed Dec 23 21:08:22 2020 -0800) on "windows_x64"

reference : https://www.geeksforgeeks.org/dart-programming-functions/?ref=rp

3 Answers 3

2

Welcome to Dart. What you are seeing is part of the new null safety feature which comes with Dart 2.12.0. You can read more about the details here: https://dart.dev/null-safety

The problem with optional variables is that they are optional. So what value do we give them if they are not set to any value? Well, you can set a default value like you do here (just want to add that this is an old syntax which are no longer recommended, you should use int g2 = 12):

void gfg3(int g1, { int g2 : 12 })

So in this case, if we don't give g2 any value it will automatically get the value 12. This is fine. What about:

void gfg2(int g1, { int g2, int g3 })

This is where it gets problematic since int is (with Dart 2.12) a type which can never be null. But the default value for a optional parameter, which are not set, is null.

What you can do is to allow g2 and g3 to be null by using the type int? which is int but allows the value null:

void gfg2(int g1, { int? g2, int? g3 })

Alternative, you can say the value is a named argument but no longer optional:

void gfg2(int g1, { required int g2, required int g3 })

Or give a default non-null value:

void gfg2(int g1, { int g2 = 0, int g3 = 0 })
Sign up to request clarification or add additional context in comments.

1 Comment

{ int g2 : 12 }... speaking of things that change over time... the modern way to do that is { int g2 = 12 }. It's now = for default value whether named or positional.
0

Pasting your code on DartPad gives me no errors and it outputs the expected. I'm curious about why would you use the dev, not the stable version? i.e use the stable version.

From the official dart website:

Dev channel releases are the most current with latest changes, may be broken, are unsupported, and may contain unvetted breaking changes.

PS: DartPad uses: Dart SDK 2.10.4

4 Comments

Use: nullsafety.dartpad.dev if you want to test with the new null-safety feature which comes with Dart 2.12.0.
i dont know how i can change stable version :( @ahmdaeyz
@KisahTegar Follow the instructions here: dart.dev/get-dart#install
You can enable NNBD dartpad with a Null Safety switch at the bottom of the page.
0

see https://dart.dev/language/functions

void func(int requiredParam, [int optionalParam = 0]){
   ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.