Home > Mobile >  How to execute a function when a condition is met in Flutter/Dart?
How to execute a function when a condition is met in Flutter/Dart?

Time:07-08

I have a function:

  //Alert Dialog about questions and answers
  void _showAlertDialog() {

    // set up the buttons
    Widget Answer1Button = TextButton(
      child: Text(_listData[_listCount][3]),
      onPressed:  () {},
    );
    Widget Answer2Button = TextButton(
      child: Text(_listData[_listCount][4]),
      onPressed:  () {},
    );

    // set up the AlertDialog
    AlertDialog alert = AlertDialog(
      // title: Text(),
      content: Text(_listData[_listCount][2]),
      actions: [
        Answer1Button,
        Answer2Button,
      ],
    );

    // show the dialog
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return alert;
      },
    );
  }

It works great when I click on the button in the different place for testing. But how to make it run under the following condition:

  void _nextCSV() {
    setState(() {
      _listData = _listData;
      _listData[_listCount][2] == "" ? _showAlertDialog : print('False');
    });
  }

Thanks in advance.

CodePudding user response:

You are missing the ()

  void _nextCSV() {
    setState(() {
      _listData = _listData;
      _listData[_listCount][2] == "" ? _showAlertDialog() : print('False');
    });
  }

But even inside the setState you can use if in case you want to.

Edited:

In case you want to ignore the then or the else block you could just put null.

  condition ? null : result;
  condition ? result : null;
  • Related