jonataslaw / get_server

A backend server that makes it possible to program with Flutter syntax and reuse existing code
Apache License 2.0
477 stars 42 forks source link

Error on the "node.js way" example #54

Closed aldycool closed 3 years ago

aldycool commented 3 years ago

Hi, I followed your example in the README.md, the "Like most of the node.js way" section, tried to run it like this (in main.dart):

import 'package:get_server/get_server.dart';

void main() {
  final app = GetServer();
  app.get('/', (ctx) => Text('Get_server of javascript way'));
  app.ws('/socket', (res) {
    res.ws.listen((socket) {
      socket.onMessage((data) {
        print('data: $data');
      });

      socket.onOpen((ws) {
        print('new socket opened');
      });

      socket.onClose((ws) {
        print('socket has been closed');
      });
    });
  });
}

And then, this error shows up:

Launching lib/main.dart on macOS in debug mode...
lib/main.dart:7:9: Error: The getter 'ws' isn't defined for the class 'BuildContext'.
 - 'BuildContext' is from 'package:get_server/src/core/server.dart' ('../../../../Apps/flutter/.pub-cache/hosted/pub.dartlang.org/get_server-1.1.0/lib/src/core/server.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'ws'.
    res.ws.listen((socket) {
        ^^
lib/main.dart:6:21: Error: A non-null value must be returned since the return type 'Widget' doesn't allow null.
 - 'Widget' is from 'package:get_server/src/core/server.dart' ('../../../../Apps/flutter/.pub-cache/hosted/pub.dartlang.org/get_server-1.1.0/lib/src/core/server.dart').
  app.ws('/socket', (res) {

It seems that the ws property isn't defined in the GetServer's BuildContext class. What I'm missing here? thanks.

farukaziz commented 3 years ago

I am facing the same problem. Here is the Stack Overflow question https://stackoverflow.com/questions/68515713/the-getter-ws-isnt-defined-get-server

jonataslaw commented 3 years ago

Fixed