9

I want to conditionally pass an argument while using the widget. Is there any way to do so?

...

return ListTile(
   title: Text("${product.name}"),
   
   // want to conditionally pass subtitle, which is not the right syntax (???)
   if(condition) subtitle: Text("subtitle"), 

   // I know about this
   subtitle: condition? Text("subtitle"): null,
 );
},

...

I know we can conditionally pass the value as null using ternary operator here but what I am looking for is to skip the argument itself.

The above example is for ListTile widget. But my curiosity is to know the syntax for doing so for any widget.

Akshay Gundewar
  • 864
  • 11
  • 30
  • Are you asking about `optional named parameters` ? Such as [this](https://stackoverflow.com/questions/13264230/what-is-the-difference-between-named-and-positional-parameters-in-dart#:~:text=Dart%20has%20two%20types%20of%20optional%20parameters%3A%20named%20and%20positional.&text=Dart's%20optional%20parameters%20are%20optional,declared%20after%20any%20required%20parameters.) ? – dev-aentgs Jun 27 '20 at 10:02
  • 1
    No. I am asking about way to include/exclude an argument while calling based on some condition. – Akshay Gundewar Jun 27 '20 at 12:15
  • Can you give another example or use case to help better understand the question? Something similar you might have used in other languages ? – dev-aentgs Jun 27 '20 at 12:18
  • Also in `optional named parameters` you can skip providing the parameter entirely. Like for ex. `Container()` can have `padding` or you can skip it. – dev-aentgs Jun 27 '20 at 12:21
  • closest to what I could do in Javascript is: ``` function func(a,b,c){ console.log(...arguments); } func(...[ 1, 2, ...(false ? [3] : []) ]); ``` – Akshay Gundewar Jun 27 '20 at 12:57
  • Case optional named parameters - I am using dart class in flutter and code is as: class MyDataObject { final int anInt; final String aString; final double aDouble; MyDataObject({ this.anInt = 1, this.aString = 'Old!', this.aDouble = 2.0, }); } getting error that need to 'Add required keyword' before this.anInt = 1, this.aString = 'Old!' and this.aDouble = 2.0, Kindly suggest what is the issue and how can we fix it. Thanks. – Kamlesh May 25 '21 at 03:33

3 Answers3

2

Using optional named parameters, using parameter:value when a parameter is required pass its value else it can be skipped completely. Inside the called method null handling is required to be done by the developer(not handled internally).

This is a simplified sample:

void main() {
  doNothing();
  doNothing(index: 1);
  doNothing(description: 'Printing');
  doNothing(index: 1,description: 'Printing');
  
}

void doNothing({int index, String description}) {
  print('Received => $index : $description');
}

Output:

Received => null : null
Received => 1 : null
Received => null : Printing
Received => 1 : Printing

Note: Default values can be assigned in the Widget/methods implementation and may not necessarily be always null.

Example:

void doNothing({int index, String description, String otherDesc = 'Provided By Default'}) {
  print('Received => $index : $description : $otherDesc ');
}

Output:

Received => null : null : Provided By Default
dev-aentgs
  • 1,218
  • 2
  • 9
  • 17
  • 1
    `null` is not necessarily the default argument supplied to optional parameters. The callee could specify something else. – jamesdlin Jun 27 '20 at 14:46
  • yes @jamesdlin, it had skipped my mind. There can be default values assigned in the `Widgets` implementation. – dev-aentgs Jun 27 '20 at 14:48
  • In your 2nd example, the `otherDesc` fallback value is used only when the parameter is not used. But what if you are using the parameter and would like to fall back to the default value if passed in value is null? – Akshay Gundewar Jun 28 '20 at 10:43
  • I think such null check needs to be added in the widget implementation but not sure how to handle this for the widgets which are not in our control. – Akshay Gundewar Jun 28 '20 at 10:48
  • 1
    @AkshayGundewar many of the inbuilt widget fallback to non null values. And yes you are right we have to be aware what we are passing. – dev-aentgs Jun 28 '20 at 11:32
0

The answer I wanted is in the question

subtitle: condition ? Text("subtitle"): null,
Yasser Nascimento
  • 1,302
  • 7
  • 13
-1

Since not explicitly initialized variables in dart get automatically initialized to null (if they don't have a default value), for the widget there is no difference between not giving an argument to the optional parameter and giving the argument null.

Because of that setting it to null is actually skipping the argument. I would recommend just using the ternary operator in case you don't want to give an argument.

capek
  • 241
  • 1
  • 7
  • Actually, it's not about this widget alone. In general, I wanted to know a way to pass arguments conditionally (for any widget). – Akshay Gundewar Jun 27 '20 at 09:48
  • Good to know this. I wasn't aware of the null behaviour in this case. Just wondering, is the null case something that is handled internally by the flutter/dart world or is it something which needs to be handled by the widget developer explicitly? – Akshay Gundewar Jun 27 '20 at 13:16
  • 5
    This is not true. If an argument is optional and is not supplied, the parameter's *default* will be used. The default is `null` if the parameter does not specify one, but it could be some `const` object. – jamesdlin Jun 27 '20 at 14:45
  • Usually the widget needs logic to handle that. In your ListTile subtitle the Widget checks if the subtitle is null and only paints if it is not. @jamesdlin Yes you are right, I have not specifically mentioned that. I changed my answer. – capek Jun 27 '20 at 15:42