from static method

TextRecord? from(
  1. NdefRecord record
)

Reads record as a text record, or returns null when it is not one or is malformed.

Implementation

static TextRecord? from(NdefRecord record) {
  if (record.typeNameFormat != NdefTypeNameFormat.wellKnown) return null;
  if (!_bytesEqual(record.type, NdefRecord.wellKnownTypeText)) return null;
  if (record.payload.isEmpty) return null;

  final status = record.payload[0];
  final languageCodeLength = status & 0x3F;
  // A truncated payload is a broken tag, not an exception: report "not a text record".
  if (record.payload.length < 1 + languageCodeLength) return null;

  final isUtf16 = (status & 0x80) != 0;
  final languageCode = ascii.decode(record.payload.sublist(1, 1 + languageCodeLength), allowInvalid: true);
  final textBytes = record.payload.sublist(1 + languageCodeLength);

  final String text;
  if (isUtf16) {
    text = _decodeUtf16(textBytes);
  } else {
    text = utf8.decode(textBytes, allowMalformed: true);
  }

  return TextRecord._(text: text, languageCode: languageCode, isUtf16: isUtf16);
}