readData method

Future<List<int>> readData({
  1. required int blockNumber,
  2. int length = BLOCK_SIZE,
})

Reads data from a specified block on a Mifare Ultralight C RFID tag.

This method communicates with the RFID tag through the RFID reader to retrieve data from a specified block. It supports reading a custom length of data, not exceeding the maximum allowed read length.

Parameters

  • blockNumber: The block number from which to start reading. Must be within the tag's memory address range.
  • length: (Optional) The number of bytes to read, defaulting to the block size. This value cannot exceed the tag's maximum read length.

Returns

A Future<List<int>> that resolves to the data read from the specified block as a list of integers.

Throws

  • Exception if the specified length is invalid (less than 0 or greater than the maximum read length).
  • RFIDException if there is an error reading data from the block, including issues like communication errors with the RFID reader or the block number being out of the allowed range.

Example

try {
  List<int> data = await card.readData(blockNumber: 4, length: 4);
  print('Data read from block 4: $data');
} catch (e) {
  print('Failed to read data: $e');
}

Note: Ensure the blockNumber and length are within the bounds of the RFID tag's memory layout to avoid errors.

Implementation

Future<List<int>> readData({
  required int blockNumber,
  int length = BLOCK_SIZE,
}) async {
  if (length < 0 || length > MAX_READ_LENGTH) {
    throw Exception('Invalid length');
  }

  _validateBlockNumber(
    blockNumber: blockNumber,
    length: length,
    start: DATA_ADDRESS_START,
    end: DATA_ADDRESS_END,
  );

  try {
    return await reader.readBlock(
      blockNumber: blockNumber,
      length: length,
    );
  } catch (e) {
    throw RFIDException('Error reading data: ${e.toString()}');
  }
}