Barcode.fromJson constructor

Barcode.fromJson(
  1. Map json
)

Returns an instance of Barcode from a given json.

Implementation

factory Barcode.fromJson(Map<dynamic, dynamic> json) {
  final type = BarcodeType.values[json['type'].toInt()];
  final format = BarcodeFormatValue.fromRawValue(json['format']);
  final displayValue = json['displayValue'];
  final rawValue = json['rawValue'];
  final rawBytes = json['rawBytes'];
  final boundingBox = json['boundingBoxLeft'] != null
      ? Rect.fromLTRB(
          (json['boundingBoxLeft']).toDouble(),
          (json['boundingBoxTop']).toDouble(),
          (json['boundingBoxRight']).toDouble(),
          (json['boundingBoxBottom']).toDouble())
      : null;
  BarcodeValue? value;
  final points = json['cornerPoints'];
  final List<Point<int>> cornerPoints = [];
  for (final point in points) {
    final cornerPoint = Point<int>(point['x'].toInt(), point['y'].toInt());
    cornerPoints.add(cornerPoint);
  }

  switch (type) {
    case BarcodeType.wifi:
      value = BarcodeWifi.fromJson(json);
      break;
    case BarcodeType.url:
      value = BarcodeUrl.fromJson(json);
      break;
    case BarcodeType.email:
      value = BarcodeEmail.fromJson(json);
      break;
    case BarcodeType.phone:
      value = BarcodePhone.fromJson(json);
      break;
    case BarcodeType.sms:
      value = BarcodeSMS.fromJson(json);
      break;
    case BarcodeType.geoCoordinates:
      value = BarcodeGeoPoint.fromJson(json);
      break;
    case BarcodeType.driverLicense:
      value = BarcodeDriverLicense.fromJson(json);
      break;
    case BarcodeType.contactInfo:
      value = BarcodeContactInfo.fromJson(json);
      break;
    case BarcodeType.calendarEvent:
      value = BarcodeCalenderEvent.fromJson(json);
      break;
    default:
      break;
  }

  return Barcode(
    type: type,
    format: format,
    value: value,
    displayValue: displayValue,
    rawValue: rawValue,
    rawBytes: rawBytes,
    boundingBox: boundingBox,
    cornerPoints: cornerPoints.isEmpty ? null : cornerPoints,
  );
}