Dart 'query' explicit cast

2019-05-17 18:45发布

问题:

Please consider following code:

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

I receive an internal error:

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');
                                        ^

So the questions is:

  • 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 ?

回答1:

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.

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;

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

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.

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:

Does query support explicit/implicit cast ?

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

Is there a query to search for ButtonElements only ?

You can write something like:

query('button#btnSendToServer')

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

Should I worried about object returned from query or I can blindly belive that object will be returned is ButtonElement ?

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