CustomAttachmentModel.fromString constructor

CustomAttachmentModel.fromString(
  1. String attachment
)

Parses an attachment from a string. Example:

CustomAttachmentModel.fromString('photo-1_2_ACCESS_KEY');

Implementation

factory CustomAttachmentModel.fromString(String attachment) {
  final parseAttachmentRe =
      RegExp(r'([a-z_]+)(-?\d+)_(\d+)_?(\w+)?', multiLine: true);

  if (!parseAttachmentRe.hasMatch(attachment)) {
    throw VkDartException('Incorrect attachment!');
  }

  final match = parseAttachmentRe.firstMatch(attachment)!;
  final attachType = match[1]!;

  final attachmentPayload = <String, dynamic>{
    'owner_id': int.parse(match[2]!),
    'id': int.parse(match[3]!),
    if (match[4] != null) 'access_key': match[4],
  };

  return AttachmentModel.fromSpecificModel(attachmentPayload, attachType)
      as CustomAttachmentModel;
}