setHeader method Null safety

void setHeader(
  1. String name,
  2. String? value,
  3. [HeaderEncoding encoding = HeaderEncoding.none]
)

Sets a header with the specified name, value and optional encoding, replacing any existing header with the same name.

Implementation

void setHeader(String name, String? value,
    [HeaderEncoding encoding = HeaderEncoding.none]) {
  headers ??= <Header>[];
  final lowerCaseName = name.toLowerCase();
  headers!.removeWhere((h) => h.lowerCaseName == lowerCaseName);
  var localValue = value;
  if (value != null) {
    if (encoding == HeaderEncoding.Q) {
      localValue = MailCodec.quotedPrintable
          .encodeHeader(value, nameLength: name.length);
    } else if (encoding == HeaderEncoding.B) {
      localValue =
          MailCodec.base64.encodeHeader(value, nameLength: name.length);
    }
  }
  headers!.add(Header(name, localValue, encoding));
}