Q:

Unable to send websocket data from flutter app to go server

I am trying to create a server-client architecture using flutter app client and go server.
Currently I am trying to send text input from flutter app, to read and print it on the server side using Websockets.
I have successfully tested my flutter app to echo from https://www.websocket.org/echo.
To connect to my local server,here is the relevant bit
For connecting to server
  final channel = IOWebSocketChannel.connect('ws://localhost:8080/test');

When onPressed,to send user entered data, sendMessage() is called;
void sendMessage(){
    if(myController.text.isNotEmpty){
      channel.sink.add(myController.text);
    }
  }

I have read the https://godoc.org/github.com/gorilla/websocket docs and other blogs to understand mux, handlefunc and listenandserve functions, but I'm not able to send data.
Here is my Go server:
package main

import (
    "fmt"
    "log"
    "net/http"
    "github.com/gorilla/websocket"
)

var upgrader = websocket.Upgrader{
        ReadBufferSize: 1024,
    }

func connectionHandler(w http.ResponseWriter, r *http.Request){
    conn, err := upgrader.Upgrade(w,r,nil)
    if err != nil {
        log.Println(err)
        return
    }

    for{
            messageType, p, err := conn.NextReader()
            fmt.Println(p)
            fmt.Println(messageType)
            if err != nil {
                log.Println(err)
                return
            }
    }

}

func main(){

    mux := http.NewServeMux()
    fmt.Printf("Connecting to server \n")

    mux.HandleFunc("/test", connectionHandler)

    log.Fatal(http.ListenAndServe(":8080", mux))

}

Edit:
Added Flutter Client code.
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:web_socket_channel/html.dart';
import 'package:web_socket_channel/io.dart';
import 'package:web_socket_channel/status.dart' as status;

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Shopping',
      initialRoute: 'home',
      theme: ThemeData(
        primarySwatch: Colors.lightBlue,
      ),
      routes: <String, WidgetBuilder>{
        'home': (BuildContext context) => HomePage(title: "Home"),
        '/shoppingpage': (BuildContext context) => ShoppingCart(title: "Shopping Cart Page"),
      },

    );
  }
}

class HomePage extends StatelessWidget {
  HomePage({Key key, this.title}) : super(key:key);
  final String title;
  final channel = IOWebSocketChannel.connect('ws://localhost:8080/test');
  TextEditingController myController = TextEditingController();
  final GlobalKey<ScaffoldState> _scaffoldkey = new GlobalKey<ScaffoldState>();

  @override
  void dispose(){
    myController.dispose();
    channel.sink.close();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldkey,
      body: Column(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          FlatButton(
              onPressed: (){
                return (Navigator.of(context).pushNamed('/shoppingpage'));
              },
              child: Text('Start Game'),
              color: Colors.blue,
              splashColor: Colors.green,

          ),

          TextField(
            controller: myController,

          ),

          FloatingActionButton(
            onPressed: (){
              sendMessage();
              _scaffoldkey.currentState.showSnackBar(SnackBar(
                content: Text('Sent'),
              ));
            },
            child: Icon(Icons.send),
          ),

          StreamBuilder(
            stream: channel.stream,
            builder: (context, snapshot){
              return snapshot.hasData ? Text(snapshot.data.toString(),
              ) : CircularProgressIndicator();
            }
          ),
        ],
      ),
    );
  }

  void sendMessage(){
    if(myController.text.isNotEmpty){
      channel.sink.add(myController.text);
    }
  }

}

class ShoppingCart extends StatelessWidget {
  ShoppingCart({Key key, this.title}) : super(key: key);
  final String title;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: BackButton(onPressed: (){
          return Navigator.pop(context);
        }),
      ),
    );
  }
}

A:

If you're running the Go server on your PC but the Flutter app on a mobile/emulator, then localhost won't work (it'll point back to the mobile/emulator, not the PC).
You'll need to use an IP address/hostname that resolves to the PC from the phone and ensure the Go server is bound to the network-accessible IP (and that there's no firewall in the way).

