'Connecting Flask Socket.IO Server and Flutter

Basically, I have a socket io flask code:

import cv2
import numpy as np

from flask import Flask, render_template
from flask_socketio import SocketIO, emit
from threading import Lock,Timer as tmr
from engineio.payload import Payload
import base64 
from PIL import Image
import io


app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)

@socketio.on('connect')
def connect():
    print("a client connected")

@socketio.on('disconnect')
def disconnect():
    print('Client disconnected')

@app.route('/')
def hello():
    return render_template('index.html')

if __name__ == '__main__':
    socketio.run(app,port=port,host= '0.0.0.0')

This code is working fine when I try to connect it to javascript

However in flutter I can not achieve that

I have stateful Widget that has a button in it And I am using this function to connect to my socket when the button is pressed

import 'package:socket_io_client/socket_io_client.dart' as IO;

IO.Socket socket;
  connectToSocket() {
  socket = IO.io("http://10.0.2.2:6400", <String, dynamic>{
    'transports': ['websocket', 'polling'],
  });
  socket.connect();
 }

I can not connect it please help me.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source