NdefRecord.createUri constructor

NdefRecord.createUri(
  1. Uri uri
)

Constructs an instance containing URI.

Implementation

factory NdefRecord.createUri(Uri uri) {
  final uriString = uri.normalizePath().toString();
  if (uriString.isEmpty) throw ('uri is empty');

  int prefixIndex =
      URI_PREFIX_LIST.indexWhere((e) => uriString.startsWith(e), 1);
  if (prefixIndex < 0) prefixIndex = 0;

  return NdefRecord(
      typeNameFormat: NdefTypeNameFormat.nfcWellknown,
      type: Uint8List.fromList([0x55]),
      identifier: Uint8List.fromList([]),
      payload: Uint8List.fromList(
        [prefixIndex] +
            utf8.encode(
                uriString.substring(URI_PREFIX_LIST[prefixIndex].length)),
      ));
}