Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9233239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:30:20+00:00 2026-06-18T06:30:20+00:00

Is there an easier (and cleaner) way to pass an optional parameter from one

  • 0

Is there an easier (and cleaner) way to pass an optional parameter from one function to another than doing this:

void optional1({num x, num y}) {
  if (?x && ?y) {
    optional2(x: x, y: y);
  } else if (?x) {
    optional2(x: x);
  } else if (?y) {
    optional2(y: y);
  } else {
    optional2();
  }
}

void optional2({num x : 1, num y : 1}) {
   ...
}

The one I actually want to call is:

void drawImage(canvas_OR_image_OR_video, num sx_OR_x, num sy_OR_y, [num sw_OR_width, num height_OR_sh, num dx, num dy, num dw, num dh])

At least I don’t get a combinatorial explosion for positional optional parameters but I’d still like to have something simpler than lot’s of if-else.

I have some code that uses the solution proposed in the first answer (propagating default values of named optional parameters, but I lose the ability to check whether or not the value was provided by the initial caller).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:30:22+00:00Added an answer on June 18, 2026 at 6:30 am

    I’ve been burned by this corner of Dart several times. My current guidelines, which I encourage anyone to adopt are:

    1. Do not use default values. Instead, use the implicit default of null and check for that in the body of the function. Document what value will be used if null is passed.
    2. Do not use the ? argument test operator. Instead, just test for null.

    This makes not passing an argument and explicitly passing null exactly equivalent, which means you can always forward by explicitly passing an argument.

    So the above would become:

    void optional1({num x, num y}) {
      optional2(x: x, y: y);
    }
    
    /// [x] and [y] default to `1` if not passed.
    void optional2({num x, num y}) {
       if (x == null) x = 1;
       if (y == null) y = 1;
       ...
    }
    

    I think this pattern is cleaner and easier to maintain that using default values and avoids the nasty combinatorial explosion when you need forward. It also avoids duplicating default values when you override a method or implement an interface with optional parameters.

    However, there is one corner of the Dart world where this doesn’t work: the DOM. The ? operator was designed specifically to address the fact that there are some JavaScript DOM methods where passing null is different from passing undefined (i.e. not passing anything).

    If you’re trying to forward to a DOM method in Dart that internally uses ? then you will have to deal with the combinatorial explosion. I personally hope we can just fix those APIs.

    But if you’re just writing your own Dart code, I really encourage you to avoid default values and ? entirely. Your users will thank you for it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an easier way than foreach($_POST as $x=>$y){ $arr[$x] = $this->input->get_post($y, TRUE); }
Is there a easier, cleaner way to write code like this: (1..10).each do |i|
Is there an easier way to do this (retaining the ability to use VIEW_VISIBLE,
Is there an easier way to get the path to an iOS application, than
I'm pretty sure there is an easier way to do this that I'm missing
Is there an easier way to prevent a duplicate insert after refresh? The way
Is there an easier way of cleaning up a database that has a ton
IS there an easier way to animate the table view. All the rows in
I am sure there are easier ways to do this, but I am not
In relation to my last question ( Is there an easier way of passing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.