Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline if assignment instead of ternary operator with null else branch #1714

Closed
petrnymsa opened this issue Jun 29, 2021 · 2 comments
Closed
Labels
feature Proposed language feature that solves one or more problems state-duplicate This issue or pull request already exists

Comments

@petrnymsa
Copy link

Use case

Many times in Flutter I've found myself writing conditional rendering using ternary operator where "else" branch returns null.

For example, a custom Widget has a parameter that will set some property of the nested widget or even add another widget to the tree.

Within Column or Row (or whenever we use array []) it is possible to use if statement, however elsewhere I am forced to use ternary operator where else does nothing other than returning null.

  1. Example - simple conditional parameter:
BoxDecoration(
    gradient: gradientColors != null
        ? LinearGradient(
            colors: gradientColors!,
          )
        : null,
)
  1. Example - Suppose I have some default Scaffold widget with shared settings and I can pass drawerBuilder to add a drawer to that Scaffold
return Scaffold(
      resizeToAvoidBottomInset: true,    
      ... // more shared settings etc...
      body: Padding(
        padding: padding,
        child:  ...., // shortened
      ),
      drawer: drawerBuilder != null
          ? Builder(builder: (context) => drawerBuilder!(context))
          : null,
    );

Proposal

Probably this request will need support from dart-lang itself. I am coming from React background and in JS / TS is possible to use && operator for such inline rendering without an explicit else branch. E.g, example 2 could be rewritten into

return Scaffold(
      drawer: drawerBuilder != null &&  Builder(builder: (context) => drawerBuilder!(context))l,
    );

It is maybe just personal preference, but I found this more clear than cluttering : null with a ternary operator.

@petrnymsa petrnymsa added the feature Proposed language feature that solves one or more problems label Jun 29, 2021
@eernstg
Copy link
Member

eernstg commented Jun 29, 2021

@petrnymsa, note that this issue might be a duplicate of other proposals, or at least similar.

In particular, #361 proposes a !! b meaning a == null ? null : b, so you could do this:

...
BoxDecoration(
    gradient: gradientColors !!
        LinearGradient(
          colors: gradientColors!, // `!` only needed if `gradientColors` is not a local variable.
        ),
)

This comment mentions some other proposals in the same area.

@petrnymsa
Copy link
Author

@petrnymsa, note that this issue might be a duplicate of other proposals, or at least similar.

In particular, #361 proposes a !! b meaning a == null ? null : b, so you could do this:

...
BoxDecoration(
    gradient: gradientColors !!
        LinearGradient(
          colors: gradientColors!, // `!` only needed if `gradientColors` is not a local variable.
        ),
)

This comment mentions some other proposals in the same area.

Thanks! I've tried to find similar issues as I was nearly sure there should be already some discussion about this topic, but failed to found it :)

Feel free to close this issue in favour of #361

@lrhn lrhn closed this as completed Jun 30, 2021
@lrhn lrhn added the state-duplicate This issue or pull request already exists label Jun 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems state-duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants