Dart Optional NAMED Parameters in Functions. Dart Tutorial for Flutter #6.5

preview_player
Показать описание

Dart beginners guide. Dart has various Function Parameters such as Required Parameters, Optional Positional Parameters, Optional Named Parameters and Optional Default Parameters. In this video we will explore Optional Named Parameters.

.
Please donate and support my work
(If you think my free tutorials are better than paid ones :)

Free Programming courses:

Free Flutter course:

Free Android courses:

More free programming courses:

Check out my website:

Let's get in touch! [Sriyank Siddhartha]

---- Thank you for your love and support ----
Рекомендации по теме
Комментарии
Автор

As in the previous video, Dart 3.0 changes the rules. It only allows null for parameters that are a nullable type. So, in the example given, you must provide a default value for the named parameters. Thus, the function becomes: void findVolume(int length, {int breadth = 10, int height = 30}) {... With that said, you can still reorder the parameters in the caller. So, for the function I gave here, you can still call it this way: findVolume(10, height: 20, breadth: 5); And finally, Dart 3.0 will not allow a function that's declared to return an integer to return a null. So, int findVolume() won't work. You must use: void findVolume().

sdmagic
Автор

Use the Keyword 'required int breath' in named parameter, who is facing issue in latest version of dart

huzaifak_
Автор

I rushed my self to learn the flutter, for an exam about a job position in internal as mobile dev then luckily I found this great tutorial !!!!

khoroshoigra
Автор

Updated (Dart 3) version:


void main() {
var result = getVolume(2, breadth: 3, height: 10);
print("Volume is $result");
print("");
print("Circumference is ${getLength(2, height: 10, breadth: 3)}");
print("");
print("Circumference is ${getLength(2, breadth: 3)}");
}

// Positional, (named and required parameters ... between curly brackets)
int getVolume(int length, {required int breadth, required int height}) {
print("height: $height \nbreadth: $breadth \nlength: $length");
return length * breadth * height;
}

// Positional, (named, required and optional parameters ... between curly brackets)
int getLength(int length, {required int breadth, int height = 0}) {
print("length: $length \nbreadth: $breadth \nheight: $height");
return length + breadth + height;
}

nolssmit
Автор

Really good info. Just needs to be updated to reflect dart having null safety.

harrisoncorupe
Автор

In 1m 28sec
Thanks for Tip
Named Parameter
Purpose : Prevent Errors if function has large numbers of parameters
Adavantage : Sequence does not a problem

karthickrajalearn
Автор

if i make middle parameter as named parameter then it trouble shoot error

noob_gamer
Автор

Hi, i've an issue that the syntax doesn't work when i don't use required keyword next to the parameter.
How to solve it?

achmadrf
Автор

Hey @Smartherd why my IDE is saying to add a required keyword while writing named parameter??

whitehawk
Автор

int function, , , says error.. void acceptable only

NasGameOrg
Автор

void main(){

findvolume(10, breath:20, height:30);
}

findvolume(int length, {int breath, int height}){

print("lenght is $length");
print("breath is $breath");
print("height is $height");

print("volume is ${length*breath*height}");

}

ENCOUNTERED AND ERROR IN ABOVE CODE:

"The parameter 'breath' 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 'height' 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."


SOLUTION: remove "int" with-in the curly brackets before "breath" and "height"

Ex:- findvolume(int length, { breath, height}){

firasatali
Автор

Please start flutter tutorial series also.

hiteshgarg
Автор

Hi, I tried the Volume code but in the function definition it says "the parameter 'height' can't have a value of null because of it's type". That's one error.. when I removed the int from before the breadth and height, it didn't mark them as errors but in the function body breadth and height would be marked error ("A value of type num can't be assigned to variable of type int).
Please help :)

P.S: In the last video on positional parameters there was a similar error.. I had to remove String from inside the brackets
e.g: [String name3] -> was an error
[name3] -> got solved

nikhiljohnjose
Автор

hello hope your doing well can you pls tell us about function call backs in flutter or dart

rananomantariq
Автор

void main(){

findvolume(10, 20, 30);
}

int findvolume(int length, int breath, int height){

print("lenght is $length");
print("breath is $breath");
print("height is $height");

print("volume is ${length*breath*height}");

}

I encountered an error :

"The body might complete normally, causing 'null' to be returned, but the return type, 'int', is a potentially non-nullable type."

so in beginning of the function had to remove "int". then it worked.

firasatali
Автор

Hi @Smartherd. Can we use both positional and named parameters at the same time for the same parameter?

flutterwithjt
Автор

Hi
could you please start tutorial for python language?
thx

SeraphimTech_io
Автор

Since we have written ' int findVolume( ) ', why we have not written a ' return length*breadth*height ' ? And my code was showing error when i printed it in the function itself then i printed it in main ( ) and it showed no error and got executed . Here is my code sir :


void main ( ) {
var result= findVolume(10, breadth:5, height:20);
print("The Volume is $result");

}

// Optional Named Parameters
int findVolume(int length, {int breadth, int height} ) {
return length*breadth*height;
}

avi
Автор

If found error write required before int
here is the correct code

void main() {

findVolume(2, 3, height: 9);
}

void findVolume(int length, int breadth, {required int height }) { // In named Parameter Sequence doesnot matter
print(height);
print(breadth);
print(height);
print("${length * breadth *height}");

}

adeeba_rafi
Автор

from now in named parameters : is replaced by =

harshbarnwal