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.
Be aware that the Paramter builder function which takes only a name should not be used to create a parameter which is a part of an function, method or a constructor. This special function can only be used to create an constructor for Enum classes. When the use case of the class is not an enum the API raises an exception to prevent an invalid generation.
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()String valueNamed 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.
At the moment its possible that this type is not directly supported!
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()abstract void print({required String text, required String name});This type of parameter can't be mixed with optionale positional parameters
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()// Variant with nullable
void test([String? test]) {
}
// Variant with default
void test([String test = "Test"]) {
}This type of parameter can't be mixed with parameters that are required
Its not possible to change the way, how the library writes the parameters to an function, typedef, extension or and constructor. The definition of the language doesn't allows that!
Last updated