Select() method returns a warning

Issue

I need a Select All button for a textarea. It works with this code, but I get a warning from Dart2js:

querySelector('#select-all-button').onClick.listen((e) {
//e.preventDefault();
querySelector('#textarea-target').select();
});

Warning: No method named 'select' in class 'Element'.
  querySelector('#textarea-target').select();
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Why?

Solution

The return type of querySelector() is dynamic AFAIR. You need to explicitly tell the analyzer what type this will return

(document.querySelector('textarea') as TextAreaElement).select();

Try it on DartPad

Answered By – Günter Zöchbauer

Answer Checked By – Pedro (FlutterFixes Volunteer)

Leave a Reply

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