13

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.

6
  • Are you asking about optional named parameters ? Such as this ? Commented Jun 27, 2020 at 10:02
  • 1
    No. I am asking about way to include/exclude an argument while calling based on some condition. Commented Jun 27, 2020 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 ? Commented Jun 27, 2020 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. Commented Jun 27, 2020 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] : []) ]); ``` Commented Jun 27, 2020 at 12:57

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

5 Comments

null is not necessarily the default argument supplied to optional parameters. The callee could specify something else.
yes @jamesdlin, it had skipped my mind. There can be default values assigned in the Widgets implementation.
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?
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.
@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.
2

The answer I wanted is in the question

subtitle: condition ? Text("subtitle"): null,

Comments

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.

4 Comments

Actually, it's not about this widget alone. In general, I wanted to know a way to pass arguments conditionally (for any widget).
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?
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.
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.

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.