flutter-nfc-manager icon indicating copy to clipboard operation
flutter-nfc-manager copied to clipboard

Migration vom 3.x to 4.x

Open philitell opened this issue 5 months ago • 2 comments

How can i create an NdefMessage with the lastest package?

This is my old implementation for creating an ndef message in package version 3.x:

final NdefMessage message = NdefMessage(records: [
        NdefRecord.createExternal(
            Constants.nfcNdefExternalRecordAndroidDomain,
            Constants.nfcNdefExternalRecordAndroidTyp,
            Uint8List.fromList(Constants.nfcNdefExternalRecordPayload.codeUnits)),
        NdefRecord.createMime(Constants.nfcNdefMimeType, Uint8List.fromList(Constants.nfcCardPayloadText.codeUnits)),
        NdefRecord.createUri(Uri.parse(Constants.nfcUri)),
      ]);

The factory methods .createExternal, .createUri, .createMime are not supported in version 4.x anymore - this breaks my production app.

@okadan can you provide an example how to create a NdefMessage?

philitell avatar Jun 30 '25 09:06 philitell

Have you found a solution? I couldn't find the documentation anywhere. The example.md also shows hello world app which is not useful at all

ilhamwd avatar Oct 21 '25 16:10 ilhamwd

@ilhamwd You can create it using this function that used in the package instead:


const uriPrefixList = [
    '',
    'http://www.',
    'https://www.',
    'http://',
    'https://',
    'tel:',
    'mailto:',
    'ftp://anonymous:anonymous@',
    'ftp://ftp.',
    'ftps://',
    'sftp://',
    'smb://',
    'nfs://',
    'ftp://',
    'dav://',
    'news:',
    'telnet://',
    'imap:',
    'rtsp://',
    'urn:',
    'pop:',
    'sip:',
    'sips:',
    'tftp:',
    'btspp://',
    'btl2cap://',
    'btgoep://',
    'tcpobex://',
    'irdaobex://',
    'file://',
    'urn:epc:id:',
    'urn:epc:tag:',
    'urn:epc:pat:',
    'urn:epc:raw:',
    'urn:epc:',
    'urn:nfc:',
  ];

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

    int prefixIndex = NFCService.uriPrefixList.indexWhere((e) => uriString.startsWith(e), 1);
    if (prefixIndex < 0) prefixIndex = 0;
    return NdefRecord(
      typeNameFormat: TypeNameFormat.wellKnown,
      type: Uint8List.fromList([0x55]),
      identifier: Uint8List.fromList([]),
      payload: Uint8List.fromList(
        [prefixIndex] + utf8.encode(uriString.substring(NFCService.uriPrefixList[prefixIndex].length)),
      ),
    );
  }
x
`

saedkhaled avatar Nov 11 '25 15:11 saedkhaled