Biagio Chirico
Biagio Chirico

Reputation: 656

Use value of DOM element

Before of the latest update, i wrote this code for example

<input type="text" id="link">
<p> {{variable}} </p>
// other code
var input = query('#link');
var variable = input.value;

This snippet of code still work, but anyone know why the editor reports this message?

"value" in not a member of Element

Upvotes: 2

Views: 127

Answers (1)

Kai Sellgren
Kai Sellgren

Reputation: 30192

The Dart Editor reports that, because it thinks input variable is an instance of Element. Why? Because query() always returns an element, but the editor can't know that in your case, it's an input (of type InputElement).

You can ignore the warning. The IDE just can't know that the queried result is an InputElement, unless you tell it:

InputElement input = query('#link');

or I think this also works:

var input = query('input#link');

If you really want to tell the Dart runtime that you expect a variable to be of a certain type, you can use the cast operator: as. This has the advantage of informing the editor of the expected type, and it will also throw an error if the variable is not of the expected type.

var input = query('#link') as InputElement;

Note that Dart is a dynamic language, so I caution against using as unless you think other developers might be confused about your intent.

Upvotes: 4

Related Questions