如何取消监听 MDCDialog 事件

How to unlisten MDCDialog event

我有一个小功能可以监听 MDCDialog:closing

问题是,每次我 运行 这个函数,它都会添加一个新的监听器。
因此,我需要在使用完后删除同一个侦听器。

到目前为止,这是我所做的:

function confirm() {
    mdcAlert.open();
    // start listening
    mdcAlert.listen("MDCDialog:closing", function(event) {
        {... execute what need to be done ...}

        // stop listening (not working)
        mdcAlert.unlisten("MDCDialog:closing");
    });
}

你知道如何使用 unlisten 吗?

我不知道如何在文档中使用它:
https://material.io/develop/web/components/dialogs/
https://pub.dev/documentation/mdc_web/latest/mdc_web/MDCComponent/unlisten.html

找到解决方案。

必须传递一个内部有函数的变量。

function confirm() {
    let eventListener=function(event) {
        {... execute what need to be done ...}

        //Unlisten after execution
        mdcAlert.unlisten("MDCDialog:closing", eventListener);
    };

    mdcAlert.open();
    mdcAlert.listen("MDCDialog:closing", eventListener);
}