decodeScannedData static method

Map<String, dynamic>? decodeScannedData(
  1. String rawData
)

Takes a raw string from a QR scanner and returns a structured Map. Returns null if the data is invalid or not from this package.

Implementation

static Map<String, dynamic>? decodeScannedData(String rawData) {
  try {
    // 1. Attempt to parse the string into JSON
    final decodedData = jsonDecode(rawData);

    // 2. Verify it's a Map and contains our required keys
    if (decodedData is Map<String, dynamic> &&
        decodedData.containsKey('title') &&
        decodedData.containsKey('items')) {
      // 3. Ensure the items list is actually a List of Strings
      final List<dynamic> rawItems = decodedData['items'];
      final List<String> cleanItems = rawItems
          .map((e) => e.toString())
          .toList();

      return {'title': decodedData['title'].toString(), 'items': cleanItems};
    }

    // If keys are missing, it's not our package's QR code
    return null;
  } catch (e) {
    // If jsonDecode fails, it was just plain text or a broken QR code
    return null;
  }
}