create static method

NdefRecord create(
  1. Uri uri
)

Builds a well-known URI record, abbreviating the longest matching prefix.

Implementation

static NdefRecord create(Uri uri) {
  final uriString = uri.normalizePath().toString();
  if (uriString.isEmpty) throw ArgumentError.value(uri, 'uri', 'is empty');

  // Index 0 is the empty prefix and matches everything, so the search starts at 1 and
  // falls back to 0. Later entries are not strictly longer than earlier ones, so take the
  // longest match rather than the first.
  var prefixIndex = 0;
  for (var i = 1; i < NdefRecord.uriPrefixList.length; i++) {
    final prefix = NdefRecord.uriPrefixList[i];
    if (uriString.startsWith(prefix) && prefix.length > NdefRecord.uriPrefixList[prefixIndex].length) {
      prefixIndex = i;
    }
  }

  return NdefRecord(
    typeNameFormat: NdefTypeNameFormat.wellKnown,
    type: NdefRecord.wellKnownTypeUri,
    identifier: Uint8List(0),
    payload: Uint8List.fromList([
      prefixIndex,
      ...utf8.encode(uriString.substring(NdefRecord.uriPrefixList[prefixIndex].length)),
    ]),
  );
}