basheps
basheps

Reputation: 10604

Dart extend WebSocket

I'm trying to add a field to WebSockets by extending the class. I don't receive errors while compiling but when I try to assign the field I get a 'no such method: myCustomString' error. Snippet below. Any help is appreciated.

class WebSocketConnection2 extends WebSocketConnection {
  String myCustomString = "";
}

...Usage

wsHandler.onOpen = (WebSocketConnection2 conn) {
conn.onMessage = (data) {
  var event = data['aString'];
  switch (event) {
    case 'someCase':
      conn.myCustomString = nName; // This line throw the error.
      ...
      break;
  }
};

Upvotes: 0

Views: 210

Answers (1)

Chris Buckett
Chris Buckett

Reputation: 14398

This will be because the implementation class of the wsHandler is passing a WebSocketConnection instance. It doesn't know about your WebSocketConnection2 class.

You'll need to provide your own implementation of the wsHandler to use the WebSocketConnection2 class.

Note: although you have specified wsHandler.onOpen = (WebSocketConnection2 conn) { this is interpreted by dart as wsHandler.onOpen = (conn) { - this is optional typing in action.

Upvotes: 2

Related Questions