Web Socket Channel Using Getx Flutter

Web Socket Channel Using Getx Flutter :

socket_page.dart

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'socket_page_controller.dart';

class SocketPage extends StatelessWidget {
  final SocketController _socketController = Get.put(SocketController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Container(
            padding: EdgeInsets.symmetric(horizontal: 15.0, vertical: 50.0),
            child: Column(children: [
              TextField(
                  controller: _socketController.textController,
                  decoration: InputDecoration(
                      border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(10.0),
                      ),
                      isDense: true,
                      contentPadding: EdgeInsets.all(15.0)),
                  onEditingComplete: () {
                    _socketController.sendText();
                    FocusScope.of(context).unfocus();
                  }),
              SizedBox(height: 20.0),
              OutlinedButton(
                  onPressed: _socketController.sendText, child: Text("Send")),
              SizedBox(height: 30.0),
              StreamBuilder(
                  stream: _socketController.socket.stream,
                  builder: (context, snapshot) {
                    return Text(
                        snapshot.hasData ? '${snapshot.data}' : "No Data",
                        style: TextStyle(fontSize: 20.0));
                  })
            ])));
  }
}

socket_page_controller.dart

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:web_socket_channel/io.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

class SocketController extends GetxController {
  WebSocketChannel _socketChannel;
  TextEditingController _textController;

  WebSocketChannel get socket => _socketChannel;
  TextEditingController get textController => _textController;

  @override
  void onInit() {
    super.onInit();
    _textController = TextEditingController();
    _socketChannel = IOWebSocketChannel.connect('wss://echo.websocket.org/');
  }

  sendText() {
    if (_textController.text.isNotEmpty) {
      _socketChannel.sink.add(_textController.text);
    }
  }

  @override
  void dispose() {
    super.dispose();
    _textController.dispose();
    _socketChannel.sink.close();
  }
}

main.dart

import 'package:flutter/material.dart';
import 'socket_page.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        title: "web socket channel getx",
        home: SocketPage());
  }
}

pubspec.yaml

name: socketweb
description: A new Flutter application.

publish_to: 'none'
version: 1.0.0+1

environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons:
  web_socket_channel:
  get:

dev_dependencies:
  flutter_test:
    sdk: flutter

flutter:
  uses-material-design: true

The flutter tutorial  is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application

For more information about Flutter. visit www.fluttertutorial.in