1
votes

I was trying to use angular dart with websockets/server sent events and could not find any documentation/examples (there are some for angularJS but that seems very different for such things). A few things I tried also did not work.

Does anyone know how to do this?

Here is one version of what I tried and the error:

@NgController (
    selector: "ACdistribution",
    publishAs : "dstbn")
class ACDstbnController{
  List <WtdPres> distbn;

  void updateDstbn(List<WtdPres> newdstbn){
    distbn = newdstbn;
  }


  final dstbnsrc = new EventSource("../dstbns")
        ..onMessage.listen((event){
    List wps = JSON.decode(event.data);
    List <WtdPres> newdistbn = wps.map((wp) => new WtdPres.fromJson(wp));
    updateDstbn(newdistbn);
      });

}

The error I got in pub build was:

web/provingground.dart:55:5:
'updateDstbn' is only available in instance methods.
    updateDstbn(newdistbn);
    ^^^^^^^^^^^
1
Can you please provide more information what you try to accomplish? Some code that shows what you tried and what didn't work out as you wished would be great.Günter Zöchbauer
I have a dynamical system running on the server (in scala/play) generating certain probability distributions. I want to send info to the front end periodically, which should get updated. This works via server sent events with dom manipulation (I did this in another application). But within an angular controller, I get various errors depending on which way I try to do things.user2947436
I find it rather hard to discuss approaches on such a generic level. Could you just drop a few lines of code, for example of your controller. I can't imagine what a problem you could have. Maybe it is about async programming in general? Please add additional info to your question not as comments (code in comments is unreadable). Then add a short comment so I get notified about the update.Günter Zöchbauer
I have put in some code, and the error message for pub build.user2947436
Great! I guess you got notified about my answer?Günter Zöchbauer

1 Answers

0
votes

There are limitations on what you can do on initializers for final fields.

Can you try to put this code inside the constructor

var dstbnsrc;

ACDstbnController() {
  dstbnsrc = new EventSource("../dstbns")
        ..onMessage.listen((event){
    List wps = JSON.decode(event.data);
    List <WtdPres> newdistbn = wps.map((wp) => new WtdPres.fromJson(wp));
    updateDstbn(newdistbn);
      });
}