'How to replace ugly if constructs?

I have a method that receives a CallbackQuery callbackQuery based on the pressed built-in button. Based on the received data, I want to call several methods How can I do it right? The solution that I have at the moment is ugly, because if there are 10 or more buttons, this equals 10 conditionals. One of my solutions was a dictionary but I couldn't implement it due to calling multiple methods. Excuse me for my english, my method would be at the bottom:

private static async Task BotOnCallbackQueryReceived(ITelegramBotClient botClient, CallbackQuery callbackQuery)
        {
            string url = $"https://api.telegram.org/bot{Configuration.BotToken}/sendMessage?chat_id={Configuration.idPrivateChannelProgrammers}&text={$"{Configuration.textMessageToSend} @{callbackQuery.From.Username}."}";
           
            if (callbackQuery.Data == "testString1")
            {
                SendsAMessageToUrlApiTelegramBot(url);
                _ = SendsAMessageToTheUserAsync(botClient, callbackQuery);
            }

            if (callbackQuery.Data == "testString2")
            {
                //string url = $"https://api.telegram.org/bot{Configuration.BotToken}/sendMessage?chat_id={Configuration.idPrivateChannelItManager}&text={$"{Configuration.textMessageToSend} @{callbackQuery.From.Username}."}";
                SendsAMessageToUrlApiTelegramBot(url);
                _ = SendsAMessageToTheUserAsync(botClient, callbackQuery);
            }

            if (callbackQuery.Data == "HelpUser")
            {
                const string messageCallb = "testString3";


                await botClient.SendTextMessageAsync(
                chatId: callbackQuery.Message.Chat.Id, 
                text: $"{messageCallb}");
            }

        }


Solution 1:[1]

As explained here, you can use a string as distinction argument in a switch statement:

string str = "one";
          
// passing string "str" in 
// switch statement
switch (str) {              
    case "one":
        Console.WriteLine("It is 1");
        break;
  
    case "two":
        Console.WriteLine("It is 2");
        break;
  
    default:
        Console.WriteLine("Nothing");
        break;
}

Solution 2:[2]

Call your getbutton mehtod like this.........

TestButton().getButton(context, myTextFieldController.value.text);

It works.....Accessing the value attribute of your textfieldcontroller will fix this problem.

Solution 3:[3]

This is because initially when you call TestButton().getButton a new widget is built with initial value of myTextFieldController.text which is empty. Now further down the line when you insert text the value of myTextFieldController.text changes, but flutter is still using the widget that was built at first and therefore dialog text does not get updated. The way you can force a rebuild is by using setState when myTextFieldController.text changes. But it is best to detach the widget and dialog from one another. Right now the widget responsible for showing the dialog is a simple ElevatedButton. If it had a fairly complex ui that needed to be used in other places it's better to implement a new widget instead of having a class that returns you a widget in its methods. If you detach the widget from showDialog there won't be any need to call setState anymore. The following code has two buttons to showcase the difference. The second one just works, for the first one though you'll have to uncomment the onChanged of TextField:

class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  var myTextFieldController = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Dialog test')),
      body: ListView(
        padding: const EdgeInsets.all(20),
        children: [
          TextField(
            controller: myTextFieldController,

            /// Uncomment the following 3 lines for the first button to work
            // onChanged: (val) {
            //   // setState(() {});
            // },
            ///
          ),
          TestButton().getButton(context, myTextFieldController.text),
          ElevatedButton(
            onPressed: () => TestButton.showTestDialogue(context, myTextFieldController.text),
            child: const Text('Show Test Dialog'),
          ),
        ],
      ),
    );
  }
}

class TestButton {
  Widget getButton(BuildContext context, myText) {
    return ElevatedButton(
      child: const Text('Show Dialog'),
      onPressed: () {
        showDialog(
          context: context,
          builder: (builder) {
            return AlertDialog(
              title: Text(myText), // is always empty
            );
          },
        );
      },
    );
  }

  static Future showTestDialogue(BuildContext context, myText) {
    return showDialog(
      context: context,
      builder: (builder) {
        return AlertDialog(
          title: Text(myText),
        );
      },
    );
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Axel Kemper
Solution 2 Baimam Boukar
Solution 3 Ramin