1
votes

I have a problem and I can`t solve it. I have 2 polymer element nested.

In principal HTML page: index.html

<poly-container a="Hi">
    <poly-item b="hi"></poly-item>
</poly-container> 

In Polymer HTML definition: polycontainer.html

<poly-container>
    <template> ...
    <content></content>
</poly-container>

In Dart class: polycontainer.dart ...Have tag <content> for render external DOM; ...and the 2 classes: PolyContainer and PolyItem

...  
ContentElement CE=shadowRoot.querySelectAll('content');
List<Node> LN= CE.getDistributedNodes();

//  LN[i] is a Node type, but we know is an element with type PolyItem.

for (var i=0; i < ......) {
    PolyItem PI= ( cast ) LN[i];    // Error in time running only;
     ......
}  

...I tried several ways. I can't access to element PolyItem. Only access to Node. I can't cast.

How do it ???

1
If you think you got a good answer you should accept the answer by clicking the checkmark below the vote count. - Günter Zöchbauer

1 Answers

2
votes
PolyItem pi = (ln[i] as PolyItem); 

returns a PolyItem if it is one or throws an exception if it isn't.

to avoid the exception you can check previously

PolyItem pi;
if(ln[i] is PolyItem) {
  pi = ln[i];
}
...

Please don't use uppercase names for variables:

PI => pi
LN => ln

Its uncommon, ugly and against the Dart style guide.
Uppercase is usually used for constants and helps distinguish intention if used consistently.
You have much better chance to get a good answer if you format your question and code well.