Dart'query'显式转换 [英] Dart 'query' explicit cast

查看:145
本文介绍了Dart'query'显式转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码:

ButtonElement btnSend = (ButtonElement) query('#btnSendToServer');

我收到内部错误:

Internal error: 'http://127.0.0.1:3030/home/Seth.Ladd/Projects/DartSimpleChat/SimpleChatClient/web/out/simplechatclient.dart': Error: line 30 pos 43: semicolon expected

ButtonElement btnSend = (ButtonElement) query('#btnSendToServer');
                                        ^

所以问题是:


  • 这是正确的方式来执行显式转换吗?

  • 查询支持显式/ c> c> 或我可以盲目地相信该对象将返回 ButtonElement

  • 是否有查询仅搜索 ButtonElements

  • Is this correct way to do explicit cast ?
  • Does query support explicit/implicit cast ?
  • Should I worried about object returned from query or I can blindly belive that object will be returned is ButtonElement ?
  • Is there a query to search for ButtonElements only ?

推荐答案

Dart是一种动态类型语言。它仍然是动态的,即使你抛出类型无处不在。所以考虑到这一点,通常当你投射它意味着你想确保的东西是特定类型。

Dart is a dynamically typed language. It's still dynamic even if you throw types everywhere. So with this in mind, usually when you are casting it means you want to be sure the thing is of particular type.

在你的情况下,你想确保它a ButtonElement
您可以使用作为操作符进行类型测试:

In your case, you want to be sure it's a ButtonElement. You can use is and as operators for type testing purposes:

// You can write code to test the type yourself:
if (btnSend is! ButtonElement) throw 'Not a button';

// Or you can use "as" that will throw if the type is wrong:
var btnSend = query('#btnSendToServer') as ButtonElement;

根据情况,我使用 as 。通常我不使用作为,因为它有一个(小?)性能开销。

Depending on the case, I use is or as. Usually I do not use as, because it has a (small?) performance overhead.

可以采取,我个人更喜欢。编写代码如下:

There's another approach you can take, which I personally prefer. Write your code like this:

ButtonElement btnSend = query('#btnSendToServer');

当您开发时,以检查模式运行:

And when you are developing, run in checked mode:

dart --checked foo.dart

或者当您使用Dartium时,请了解如何手动使用旗帜启动Dartium 。我没有使用Dart编辑器一段时间,所以我不知道它是否默认使用检查模式,如果你可以改变。

Or when you use Dartium, read about launching Dartium manually with flags. I haven't used Dart Editor for a while, so I'm not sure if it uses checked mode by default and if you can change that.

模式,如果类型不匹配,则会分配到 btnSend 。好的是,当你在生产环境中运行你的代码而没有选中模式时,你的应用程序不会受到任何性能开销的影响。

When running in checked mode, the assignment to btnSend will throw if the type doesn't match. The nice thing about this is that when you run your code in production without checked mode, your application will not suffer from any performance overhead.

并回答一些单独的问题:

And to answer some individual questions:

否。它只是一个不关心类型的随机函数。

No. It's just a random function that does not care about types.

您可以这样写:

query('button#btnSendToServer')

这是一个典型的CSS选择器, 。

It's a typical CSS selector, not a Dart thing.

是和否。我相信,如果对象不是 ButtonElement ,但是我建议您在开发和编写时运行在检查模式下, / p>

Yes and no. I believe it will throw at your application at some point eventually if the object is not a ButtonElement, but I would recommend you to run in checked mode when developing and writing like:

ButtonElement btnSend = query('#btnSendToServer');

这取决于你决定要输入多少类型信息。如果你认为按钮可以容易地是错误的类型,然后我认为有意义的指定类型。就我个人而言,我不会疯狂的类型,只有在我认为他们有意义。

It's up to you to decide how much type informationg you want to throw in. If you think the button can easily be wrong type, then I think it makes sense to specify the type. Personally I don't go crazy with types, only where I think they make sense.

这篇关于Dart'query'显式转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆