fromJson static method

InputMessageContent fromJson(
  1. Map<String, dynamic> json
)

Creates a new InputMessageContent object from a JSON object. This method decides which InputMessageContent subclass to use based on the fields.

Implementation

static InputMessageContent fromJson(Map<String, dynamic> json) {
  final isText = json['message_text'] != null;
  final isLocation = json['latitude'] != null && json['title'] == null;
  final isVenue = json['latitude'] != null && json['title'] != null;
  final isContact = json['phone_number'] != null;
  final isInVoice =
      json['currency'] != null && json['provider_token'] != null;

  if (isText) {
    return InputTextMessageContent.fromJson(json);
  } else if (isLocation) {
    return InputLocationMessageContent.fromJson(json);
  } else if (isVenue) {
    return InputVenueMessageContent.fromJson(json);
  } else if (isContact) {
    return InputContactMessageContent.fromJson(json);
  } else if (isInVoice) {
    return InputInvoiceMessageContent.fromJson(json);
  } else {
    throw TeleverseException(
      'Unknown InputMessageContent type',
      description:
          'The given JSON object does not match any InputMessageContent type.',
      type: TeleverseExceptionType.invalidParameter,
    );
  }
}