Parameters

Parameters in the programming language Dart can be more challenging to understand compared to other languages. Dart has various subtypes of parameters that can be combined in certain ways, but not all variants can be used together simultaneously. To avoid issues when creating parameters with the API, you don't need to call different methods to add parameters to the correct data structure. The sorting process is handled by the spec object itself during its creation. If something is incorrect, it throws an exception to inform you about the problem.

To keep the wiki page simple, the creation of parameters is broken down by type. This approach helps to highlight the individual differences more clearly. If you encounter difficulties, you should refer to the Dart wiki for additional guidance.

A function can have any number of required positional parameters. These can be followed either by named parameters or by optional positional parameters (but not both). When a function, constructor contains more than one parameter. So each parameter would be separated by an comma. Taken from the offical wiki

Required positional parameters:

This type of parameter is the standard variant found in most programming languages. It does not use any special keywords, default values, or special brackets. These parameters are simply placed within the normal round brackets ().

val stringParamter = ParameterSpec.builder("value", String::class).build()

Named parameters:

These parameters are optional unless marked as required. In a function or constructor, they are enclosed in {} brackets. Named parameters have some important characteristics to note. If they do not have a default value or are not marked as required, their types must be nullable, and their default value will be null.

Required parameters:

Required parameters in Dart enhance code clarity by clearly indicating which inputs are essential for a function. This improves readability and helps other developers understand the necessary arguments at a glance. As a result, the overall code becomes easier to maintain and work with. In a function, constructur etc. these are encolosed in `{}` brackets like named parameters.

FunctionSpec.builder("print")
    .modifiers(DartModifier.ABSTRACT)
    .parameters(
        ParameterSpec.builder("text", String::class).required().build(),
        ParameterSpec.builder("name", String::class).required().build()
     )
     .build()

A parameter that is marked as `required` can be still nullable

Optional positional parameters:

These parameters are enclosed in [] brackets, marking them as optional positional parameters. This means that these parameters do not need to be specified when the function is called. For comparison, you can think of them like parameters in Kotlin that have a default value and do not need to be specified in a constructor. However, in Dart, they are slightly different. If you do not provide a default value, the parameter's type must be nullable. When a parameter is marked as nullable, its default value is null by default.

val testParameter: ParameterSpec = ParameterSpec.builder("test", String::class)
    .nullable(true) // Marks them as nullable, adds a ? to the generated parameter
    //.initializer("%S", "Test") // Use this for a parameter with a default value
    .build()
val testFunction: FunctionSpec = FunctionSpec("test")
    .parameter(testParameter)
    .build()

Last updated