Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Define Optional Named and Positional Parameters for Functions and Methods in Dart

    Jermaine OppongJermaine Oppong

    In this lesson, we will look at the different forms of creating parameters that are optional, namely positional and named. Optional positional arguments are useful when specifying a default value for a particular parameter. Optional named parameters are useful for specifying parameter defaults and writing self-documenting functions and methods.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Instructor: Apart from parameters that are required, functions or methods in Dart can also specify optional parameters. These come in two flavors, positioning and named. The required parameters are listed first, followed by any of the optional parameters. Optional positional parameters are defined inside opening and closing square brackets, separated by commas.

    The exclaim parameter defaults to null. Let's specify a default Boolean, and add another parameter. Optional named parameters are defined inside opening and closing curly brackets.

    Using optional named parameters will help you define functions and methods that are self-documenting. Here's how you would use these in a case of methods. Let's invoke our method. Let's refactor our method example with named parameters, update the method call, run again.