writeMemoizedGetter function

void writeMemoizedGetter({
  1. required StringBuffer buffer,
  2. required String getterName,
  3. required String returnType,
  4. required String code,
  5. required GenerationOptions options,
  6. bool hasOverride = false,
})

Writes the following dart code into the buffer:

ReturnType _getterName;
ReturnType get getterName => _getterName ??= code;

When we're writing nnbd-code, the following code will be emitted instead:

late final ReturnType getterName = code;

This means that code should be an expression without any trailing semicolon.

Implementation

void writeMemoizedGetter(
    {required StringBuffer buffer,
    required String getterName,
    required String returnType,
    required String code,
    required GenerationOptions options,
    bool hasOverride = false}) {
  if (options.nnbd) {
    if (hasOverride) {
      buffer.write('@override\n');
    }
    buffer.writeln('late final $returnType $getterName = $code;');
  } else {
    buffer.write('$returnType _$getterName;\n');
    if (hasOverride) {
      buffer.write('@override\n');
    }
    buffer.write('$returnType get $getterName => _$getterName ??= $code;');
  }
}