writeData method

Future<void> writeData({
  1. required int blockNumber,
  2. required List<int> data,
})

Writes data to a specified block on a Mifare Ultralight C RFID tag.

This method allows writing a predefined list of byte data to a specific block within the RFID tag's memory. It ensures that the data length matches the block size and validates the data bytes against the tag's specifications.

Parameters

  • blockNumber: The block number where the data should be written. It must fall within the valid data address range of the tag.
  • data: A list of integers representing the data to be written. The length of this list must exactly match the tag's block size.

Throws

  • InvalidDataException if the data length does not match the required block size or if any byte in the data list is outside the valid byte range (0x00 to 0xFF).
  • RFIDException if there is an error during the write operation, such as communication issues with the RFID reader or validation failures.

Example

try {
  await card.writeData(blockNumber: 4, data: [0x01, 0x02, 0x03, 0x04]);
  print('Data successfully written to block 4');
} catch (e) {
  print('Failed to write data: $e');
}

Note: This method enforces strict validation of the data length and byte values to ensure compatibility with the Mifare Ultralight C tag's specifications. Ensure that the blockNumber and data are correctly specified to avoid errors.

Implementation

Future<void> writeData({
  required int blockNumber,
  required List<int> data,
}) async {
  _validateBlockNumber(
    blockNumber: blockNumber,
    start: DATA_ADDRESS_START,
    end: DATA_ADDRESS_END,
  );

  if (data.length != BLOCK_SIZE) {
    throw InvalidDataException(
        'Invalid data length. Must be equal to $BLOCK_SIZE');
  }

  try {
    validateByteList(data);
  } catch (e) {
    throw InvalidDataException(
        'Invalid data. Each byte must be within 0x00 and 0xFF');
  }

  try {
    await reader.writeBlock(blockNumber: blockNumber, data: data);
  } catch (e) {
    throw RFIDException(
      'Error writing data to block $blockNumber: ${e.toString()}',
    );
  }
}