Named parameter are not working in this code can anyone help me with this .I took this code from geeksofgeeks

16 views
Skip to first unread message

Baldev Singh

unread,
Oct 25, 2022, 2:01:02 AM10/25/22
to Dart Misc
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);
}
Error:
lib/main.dart:2:25: Error: The parameter 'g2' can't have a value of 'null' because of its type 'int', but the implicit default value is 'null'. void gfg1(int g1, [ int g2 ]) ^^ lib/main.dart:9:25: Error: The parameter 'g2' can't have a value of 'null' because of its type 'int', but the implicit default value is 'null'. void gfg2(int g1, { int g2, int g3 }) ^^ lib/main.dart:9:33: Error: The parameter 'g3' can't have a value of 'null' because of its type 'int', but the implicit default value is 'null'. void gfg2(int g1, { int g2, int g3 }) ^^ Error: Compilation failed.

Brett Sutton

unread,
Oct 25, 2022, 2:42:06 AM10/25/22
to mi...@dartlang.org
Looks like the code was written pre NNBD.

change 
void gfg1(int g1, [ int g2 ])

to 
void gfg1(int g1, [ int? g2 ])

The same for any other optional parameters unless you provide a default value.



S. Brett Sutton
Noojee Contact Solutions
03 8320 8100


--
For more ways to connect visit https://dart.dev/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/3937eda9-6258-4a4e-9c1f-0555c9e47cf8n%40dartlang.org.

Shlomo Belleli

unread,
Oct 25, 2022, 2:55:54 AM10/25/22
to Dart Misc, bs36...@gmail.com
Here is your code running just fine in DartPad
you missed the "?" operator for the  null safety

here 
  void gfg1(int g1, [ int? g2 ])
and here
  void gfg2(int g1, { int? g2, int? g3 })



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);
}

Reply all
Reply to author
Forward
0 new messages