Hello I am trying to run following code, I want to run a specific asynchronous code and show alert dialog until it's running. But the code is not being executed after await showAlertDialog(); this line.

  void appendAndRunPythonCode() async {
    await showAlertDialog();
    await runPythonScript(final_code);
    _alertDialogUtils.dismissAlertDialog(context);
  }

This is how my showAlertDialog() function is implemented:

Future<void> showAlertDialog() async {
    if (!_alertDialogUtils.isShowing) {
      await _alertDialogUtils.showAlertDialog(context);
    }
  }

runPythonCode():

Future<void> runPythonScript(String code) async {
    if (inputImg == null) {
      ToastUtils.showToastMessage(text: ConstUtils.input_image_empty_notice);
      return;
    }

    if (code.isEmpty) {
      ToastUtils.showToastMessage(text: ConstUtils.code_empty);
      return;
    }

    List<String> lines = code.split('\n');
    String lastLine = lines.elementAt(lines.length - 4);

    if (lastLine.split(' ').elementAt(0).compareTo('outputImage') != 0) {
      ToastUtils.showToastMessage(text: ConstUtils.cv_error_line2);
      return;
    }

    data.putIfAbsent("code", () => code);
    data.putIfAbsent("inputImg", () => inputImg);
    _alertDialogUtils.showAlertDialog(context);

    final result = await _channel.invokeMethod("runPythonCVScript", data);
    //  Add Artifical Delay of 3 seconds..
    await Future.delayed(
      Duration(seconds: 3),
    );
    _alertDialogUtils.dismissAlertDialog(context);

    setState(
      () {
        _scrollController.animateTo(
          _scrollController.position.maxScrollExtent,
          curve: Curves.easeOut,
          duration: const Duration(milliseconds: 300),
        );
        output = result['textOutput'] ??= "";
        error = result['error'] ??= "";
        outputImg = (result['graphOutput']);
        data.clear();
      },
    );
  }


Solution 1: Josteve

You shouldn't await the showAlertDialog because runPythonScript won't be executed until the dialog is dismissed.

Remove the await. Like so:

 void appendAndRunPythonCode() async {
    showAlertDialog();
    await runPythonScript(final_code);
    _alertDialogUtils.dismissAlertDialog(context);
  }