create static method

NdefRecord create(
  1. String text, {
  2. String languageCode = 'en',
})

Builds a well-known text record. Always encodes as UTF-8.

Implementation

static NdefRecord create(String text, {String languageCode = 'en'}) {
  // Checked before encoding rather than left to `ascii.encode`, whose own
  // "Contains invalid characters." names neither the parameter nor the rule. An IANA
  // language tag is ASCII by definition, so this rejects nothing that was ever valid.
  if (languageCode.codeUnits.any((unit) => unit > 0x7F)) {
    throw ArgumentError.value(languageCode, 'languageCode', 'is not an ASCII IANA language tag');
  }
  final languageCodeBytes = ascii.encode(languageCode);
  // The status byte packs the length into six bits, so 63 is the ceiling.
  if (languageCodeBytes.length > 63) throw ArgumentError.value(languageCode, 'languageCode', 'is too long');

  return NdefRecord(
    typeNameFormat: NdefTypeNameFormat.wellKnown,
    type: NdefRecord.wellKnownTypeText,
    identifier: Uint8List(0),
    payload: Uint8List.fromList([languageCodeBytes.length, ...languageCodeBytes, ...utf8.encode(text)]),
  );
}