Placeholders
Most of the API from DartPoet uses immutable objects from Kotlin. There are also builder, method chaining and other parts to make the API as friendly as possible.
DartPoet contains
%S or %C for Strings
When emitting code that includes string literals, we can use %S
or %C
to emit a string
, complete with wrapping quotation marks and escaping.
Example which emits the string with %S
:
Creates this function:
%T for Types
When you want to use a type for the generation, you can use %T
. Note: DartPoet can't generate an import for that class automatically. This must be done by the user.
Results in this generated function:
Nullable Types
The written code for Dart should ideally be Null-Safe. Specifically, this means avoiding values that can be null. However, it is still possible to define variables as nullable. When creating parameters or variables, one must explicitly state that the value can be nullable.
To create a nullable value, we need to make a difference between type which are parameterized or not:
Parameterized values:
Which returns the following generated variable:
%L for Literals
Results in the following generation:
Literals are emitted directly to the output code with no escaping. Arguments for literals may be strings, primitives, and a few KotlinPoet types described below.