Ad

Socket_io_client: ^1.0.1 Flutter Web App Cannot Connect

This is the code I wrote . This code works fine in ios and android devices. I can connect in android/ios devices

But I get this error in flutter web(chrome):

{msg: websocket error, desc: null, type: TransportError}

This is my code :

import 'package:socket_io_client/socket_io_client.dart' as IO;
void setupSocketIO() {
IO.Socket socket = IO.io('ws://151.240.60.171:81',
  OptionBuilder().setTransports(['websocket']).build());

socket.onConnect((_) {
  print('connect');
});

socket.onConnectError((data) {
  print(data);
});

socket.onDisconnect((_) => print('disconnect'));

}
Ad

Answer

Try removing .setTransports(['websocket']). I don't think that part is meant to be used on Flutter Web since it uses dart:html. Maybe like

if (kIsWeb) {
  //run without ".setTransports"..
} else {
  //run with ".setTransports"...
}
Ad
source: stackoverflow.com
Ad