fromJson<C extends NearbyMessageContent> static method

C fromJson<C extends NearbyMessageContent>(
  1. Map<String, dynamic>? json
)

Contains the conditional logic of creating NearbyMessageFilesRequest, NearbyMessageFilesResponse or NearbyMessageTextRequest by type field of json.

Implementation

static C fromJson<C extends NearbyMessageContent>(
  Map<String, dynamic>? json,
) {
  try {
    final type = NearbyMessageContentType.values.firstWhere(
      (e) => e.name == json?['type'],
    );
    if (type.isTextRequest) {
      return NearbyMessageTextRequest.fromJson(json) as C;
    } else if (type.isTextResponse) {
      return NearbyMessageTextResponse.fromJson(json) as C;
    } else if (type.isFilesRequest) {
      return NearbyMessageFilesRequest.fromJson(json) as C;
    } else if (type.isFilesResponse) {
      return NearbyMessageFilesResponse.fromJson(json) as C;
    } else {
      throw NearbyServiceException.unsupportedDecoding(json);
    }
  } catch (e) {
    throw NearbyServiceException(e);
  }
}