How to get element assoicated to onClick event

Issue

I’m very new to Dart, and I’m encountering a problem with this code:

DivElement badge = querySelector('.badge');
badge.onClick.listen(onBadgeClick);

The event handler looks like this:

void onBadgeClick(MouseEvent e){
    print(e.relatedTarget);
}

I get this exception

Exception: Unsupported operation: Cannot call matchingTarget if this
Event did not arise as a result of event delegation.

How can I get the element that the click is associated with?

Solution

e.target should you give the element that created the event. If you set a breakpoint in DartEditor the code execution halts on the line with the breakpoint and you can investigate the properties of the e instance.

Answered By – Günter Zöchbauer

Answer Checked By – Robin (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *