Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

❄️ Freeze #318

Merged
merged 4 commits into from
Mar 4, 2025
Merged

❄️ Freeze #318

merged 4 commits into from
Mar 4, 2025

Conversation

HeySreelal
Copy link
Member

@HeySreelal HeySreelal commented Feb 17, 2025

Tracked in #319.

@HeySreelal HeySreelal requested a review from im-trisha February 17, 2025 06:30
@HeySreelal
Copy link
Member Author

HeySreelal commented Feb 17, 2025

Keyboard & InlineKeyboard Case

The Keyboard and InlineKeyboard markups were mutable, now with freezed, they're no longer mutable. Hence the following code (which used to work) no longer works.

void main(List<String> args) async {
  final keyboard = Keyboard();
  keyboard.text("Click Me");
  keyboard.requestLocation("Send Me Location");

  await api.sendMessage(
    chatId,
    "Use the buttons please.",
    replyMarkup: keyboard,
  );
}

Solution

1. Update the keyboard variable on each change

Just like how we use copyWith method :)

void main(List<String> args) async {
  var keyboard = Keyboard();
  keyboard = keyboard.text("Click Me");
  keyboard = keyboard.requestLocation("Send Me Location");

  await api.sendMessage(
    chatId,
    "Use the buttons please.",
    replyMarkup: keyboard,
  );
}

2. Chain it or spread it.

void main(List<String> args) async {
  var keyboard =
      Keyboard().text("Click Me").requestLocation("Send Me Location");

  await api.sendMessage(
    chatId,
    "Use the buttons please.",
    replyMarkup: keyboard,
  );
}

3. (Untested)

Or may be we can do Keyboard and InlineKeyboard classes to be @unfreezed to make it mutable? May be that works?

  • Test this

@im-trisha
Copy link
Collaborator

Keyboard & InlineKeyboard Case

The Keyboard and InlineKeyboard markups were mutable, now with freezed, they're no longer mutable. Hence the following code (which used to work) no longer works.

void main(List<String> args) async {
  final keyboard = Keyboard();
  keyboard.text("Click Me");
  keyboard.requestLocation("Send Me Location");

  await api.sendMessage(
    chatId,
    "Use the buttons please.",
    replyMarkup: keyboard,
  );
}

Solution

1. Update the keyboard variable on each change

Just like how we use copyWith method :)

void main(List<String> args) async {
  var keyboard = Keyboard();
  keyboard = keyboard.text("Click Me");
  keyboard = keyboard.requestLocation("Send Me Location");

  await api.sendMessage(
    chatId,
    "Use the buttons please.",
    replyMarkup: keyboard,
  );
}

2. Chain it or spread it.

void main(List<String> args) async {
  var keyboard =
      Keyboard().text("Click Me").requestLocation("Send Me Location");

  await api.sendMessage(
    chatId,
    "Use the buttons please.",
    replyMarkup: keyboard,
  );
}

3. (Untested)

Or may be we can do Keyboard and InlineKeyboard classes to be @unfreezed to make it mutable? May be that works?

* [ ]  Test this

The second solution LGTM! It should be the best one, as it doesn't assign the variable again and again (1) and doesn't box/unbox the object every time (3) but just uses the copyWith method :)

@HeySreelal HeySreelal marked this pull request as draft February 17, 2025 17:27
@HeySreelal HeySreelal linked an issue Feb 24, 2025 that may be closed by this pull request
5 tasks
@HeySreelal HeySreelal marked this pull request as ready for review March 4, 2025 06:12
@HeySreelal HeySreelal merged commit 7d330f6 into main Mar 4, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rewrite all Telegram models with Freezed!
2 participants