如何告诉 dartanalyzer 所选对象是哪种元素?

How can I tell the dartanalyzer what kind of Element the selected object is?

我有密码

if(querySelector('#username').value!="")

它正在运行。但是 Darteditor 警告我,getter 'value' 没有为 class 'Element' 定义。我知道,querySelector returns 是一个 InputElement,因此,我的问题只是为了美观和删除警告。我如何告诉分析器它可以期待一个 InputElement?

自从我上次使用 C# 以来已有 2 年多了,但我相信有一种类似于 ((InputElement) (myvariable).value 的语法(我完全不确定它的样子。)告诉调试器 myvariable 是被视为 InputElement,即使它被定义为 Element。 Dart 中是否有类似的东西适合我的情况,以便我可以做这样的事情?

if(((InputElement) querySelector('#username')).value!="")

抱歉,我在 google 中没有找到任何内容 - 可能是错误的搜索词。 提前谢谢你:)

您可以在 Dart 中使用 as 到 "cast"。

import 'dart:html';
...
if((querySelector('#username') as InputElement).value!="")

另一种不需要运行时类型检查的选项是将值分配给类型化的临时变量:

InputElement usernameInput = querySelector('#username');
if (usernameInput.value != "") ...

这也为该值提供了 InputElement 的静态类型,但它仅在运行时以检查模式进行类型检查,as 每次都进行。 不过,类型检查不太可能成为 querySelector 调用旁边的瓶颈。