I want to display data realtime on ListDemo widget with the socket value using getx, but I having trouble to implement
// socket.dart
class WebSocket {
final hubConnection = HubConnectionBuilder().withUrl(EnvironmentHml.socketUrlFake).build();
SocketController socketController = Get.put(SocketController());
createWS() async {
await hubConnection.start();
hubConnection.on("ReceiveMessage", listenWSMessages);
}
listenWSMessages(List<Object> result) { // simple ws listener
socketController.wsData = result[1];
}
}
// socket_controller.dart
class SocketController extends GetxController {
var wsData = ''.obs;
}
my widget:
class ListDemo extends StatelessWidget {
final ListDemoController listDemoController = Get.put(ListDemoController());
@override
Widget build(BuildContext context) {
return Container(
child: Text('display data here'),
);
}
}
widget controller:
class ListDemoController extends GetxController {
WebSocket webSocket = WebSocket(); // create a websocket
@override
void onInit() {
super.onInit();
webSocket.createWS(); // start websocket
}
}
the websocket connection works, but at this point i dont know what to do
ok, i finally get the solution, basically what i made is:
font: https://pub.dev/packages/get