0

In the following code f.onChildAdded is an Instance of '_BroadcastStream<Event>':

f.onChildAdded.forEach((e) {
  print(e.snapshot.val());
  //TODO: Turn this somehow into a list
 });

The print outputs each item fine, and now I'd like to turn it into a list that's friendly with Polymer's repeat="{{item in items }}" magic.

  • How do I turn it into a list? List myList = new List(); and myList.add(e.snapshot.val()); isn't helping me there.
  • How do I pass that from my main() in my index.dart to my-element.dart?

FWIW, here's my project: https://github.com/DaveNotik/dartstack. Any other guidance as I get a handle on this would be great!

David Notik
  • 2,026
  • 15
  • 21
  • You should move your entire code in `main` but at least the code that accesses some Polymer element into the method body of ` Polymer.onReady.then((_) {`. It's not enough to have this `Polymer.onReady` in your code, it's important that your code is executed **by** this code. – Günter Zöchbauer Jul 04 '14 at 10:18

1 Answers1

0

At first you should ensure that your main look like shown here how to implement a main function in polymer apps

in your main:

List myList = new List();

f.onChildAdded.forEach((e) {
  myList.add(e.snapshot.val());
});

(querySelector('your-element') as YourElement).myList = myList;

or when you don't want to create a new list each time:

The list field in you custom element should look like

@observable List myList = toObservable([]);

in your main:

List myList = (querySelector('your-element') as YourElement).myList;

f.onChildAdded.forEach((e) {
  myList.add(e.snapshot.val());
});
Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • `malformed type: line 29 pos 53: type 'InboxList' is not loaded List inboxItems = (querySelector('inbox-list') as InboxList).inboxItems;` I updated my question with a link to my code (prior to this edit). – David Notik Jul 04 '14 at 10:24
  • See my comment to your question, I guess this is the cause of this error. Your repo seems not to be up-to-date. I found no line with `querySelector('inbox-list')...` – Günter Zöchbauer Jul 04 '14 at 10:34
  • I hadn't pushed the broken stuff. Just did. Still errors, including the one above. – David Notik Jul 04 '14 at 10:53
  • To make this work ` as InboxList` you need to import the dart file of your Polymer element into `index.dart` (in addition to the import in your `index.html`). `import 'packages:dartstack/components/inbox-list/ inbox-list.dart`. Hint: you should not use a dash `-` in file names in Dart. Use the underscore instead. – Günter Zöchbauer Jul 04 '14 at 10:58
  • Added: `import 'package:../../lib/components/inbox_list/inbox_list.dart';`. Got: `Uncaught Error: type 'InboxList' is not a subtype of type 'InboxList' in type cast.` Pushed updates. – David Notik Jul 04 '14 at 18:55
  • Found elsewhere: "This kind of error are commonly the result of not having a single canonical way to import some library. For example, if a library is imported from the main entry point by going through a 'package:' url and separately going only through relative urls. In such cases Dart treats them as different libraries that happen to use the same name for some types." Looking into how I'm importing. – David Notik Jul 04 '14 at 18:58