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 8789143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:19:12+00:00 2026-06-13T22:19:12+00:00

Dart supports both named optional parameters and positional optional parameters. What are the differences

  • 0

Dart supports both named optional parameters and positional optional parameters. What are the differences between the two?

Also, how can you tell if an optional parameter was actually specified?

  • 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-13T22:19:14+00:00Added an answer on June 13, 2026 at 10:19 pm

    Dart has two types of optional parameters: named and positional. Before I discuss the differences, let me first discuss the similarities.

    Dart’s optional parameters are optional in that the caller isn’t required to specify a value for the parameter when calling the function.

    Optional parameters can only be declared after any required parameters.

    Optional parameters can have a default value, which is used when a caller does not specify a value.

    Positional optional parameters

    A parameter wrapped by [ ] is a positional optional parameter. Here is an example:

    getHttpUrl(String server, String path, [int port=80]) {
      // ...
    }
    

    In the above code, port is optional and has a default value of 80.

    You can call getHttpUrl with or without the third parameter.

    getHttpUrl('example.com', '/index.html', 8080); // port == 8080
    getHttpUrl('example.com', '/index.html');       // port == 80
    

    You can specify multiple positional parameters for a function:

    getHttpUrl(String server, String path, [int port=80, int numRetries=3]) {
      // ...
    }
    

    The optional parameters are positional in that you can’t omit port if you want to specify numRetries.

    getHttpUrl('example.com', '/index.html');
    getHttpUrl('example.com', '/index.html', 8080);
    getHttpUrl('example.com', '/index.html', 8080, 5);
    

    Of course, unless you know what 8080 and 5 are, it’s hard to tell what those apparently magic numbers are. You can use named optional parameters to create more readable APIs.

    Named optional parameters

    A parameter wrapped by { } is a named optional parameter. Here is an example:

    getHttpUrl(String server, String path, {int port = 80}) {
      // ...
    }
    

    You can call getHttpUrl with or without the third parameter. You must use the parameter name when calling the function.

    getHttpUrl('example.com', '/index.html', port: 8080); // port == 8080
    getHttpUrl('example.com', '/index.html');             // port == 80
    

    You can specify multiple named parameters for a function:

    getHttpUrl(String server, String path, {int port = 80, int numRetries = 3}) {
      // ...
    }
    

    Because named parameters are referenced by name, they can be used in an order different from their declaration.

    getHttpUrl('example.com', '/index.html');
    getHttpUrl('example.com', '/index.html', port: 8080);
    getHttpUrl('example.com', '/index.html', port: 8080, numRetries: 5);
    getHttpUrl('example.com', '/index.html', numRetries: 5, port: 8080);
    getHttpUrl('example.com', '/index.html', numRetries: 5);
    

    I believe named parameters make for easier-to-understand call sites, especially when there are boolean flags or out-of-context numbers.

    Checking if optional parameter was provided

    Unfortunately, you cannot distinguish between the cases “an optional parameter was not provided” and “an optional parameter was provided with the default value”.

    Note: You may use positional optional parameters or named optional parameters, but not both in the same function or method. The following is not allowed.

    thisFunctionWontWork(String foo, [String positonal], {String named}) {
      // will not work!
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

http://www.dartlang.org/docs/spec/dartLangSpec.pdf The language spec for Dart mentions below Dart supports optional typing based on
How can I made a JSON string out of a collection in dart, as
Given something like: import 'dart:math'; I can't seem to find a way to get
I wonder how does Dart handle JSON? More specifically: Can I access item in
I've start using the Dart plugin for webstorm and wonder how can I open
How can the Google Closure UI Library be used with Google DART?
I am experimenting with Dart. I have created an on click event like so:
I'm disappointed that dart doesn't feature weak references. Is there something about compiling to
I was considering using Dart for a Framework I was going to write. One
It looks like Google's Dart language doesn't allow you to call native js functions

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.