ID.create constructor

ID.create(
  1. dynamic value
)

Creates a new ID object with the passed value.


Easter Egg! 🥚 You've found the only place where Televerse uses dynamic type on public interface. We highly encourage you to rely on ChatID or ChannelID classes to create instance of ID but yeah, this can be quite an easy one.


This method will return:

  • a ChatID if the passed value is an integer or a string that can be parsed to an integer value
  • a ChannelID if the passed value is a string that cannot be parsed to int.

If the passed value is neither an integer nor a string, this method will throw a TeleverseException.

Implementation

factory ID.create(dynamic value) {
  if (value is int) {
    return ChatID.create(value);
  }

  if (value is String) {
    final parsed = int.tryParse(value);
    if (parsed != null) return ChatID(parsed);
    return ChannelID.create(value);
  }

  throw TeleverseException(
    "The passed value is not a valid chat id. The value must be an integer or a string.",
    description: "The passed value is of type ${value.runtimeType}.",
    type: TeleverseExceptionType.invalidParameter,
  );
}