title: Socket.io connection works only once
body: <p>I use <a href="https://github.com/rikulo/socket.io-client-dart" rel="nofollow noreferrer">this</a> library (socket.io) in my flutter app for instant messaging using web sockets.</p>
  
  <p>There are 2 screens - <code>Home</code> and <code>Chat</code>. After a form input from <code>Home</code> screen, the app navigates to <code>Chat</code> screen, inside <code>initState()</code> method of which, I have written the code to establish the socket connection:</p>
  
  <pre><code>IO.Socket socket;
  void initState() {
      socket = IO.io('http://localhost:3000', &lt;String, dynamic&gt;{
          'transports': ['websocket'],
          'query': {
              'timeStamp': new DateTime.now().millisecondsSinceEpoch
          }
      });
  
      socket.on('connect', (_) {
           print("debug: connected");
      });
  });
  
  </code></pre>
  
  <p>The connection works the first time I navigate from <code>Home</code> to <code>Chat</code>. But, If I navigate back to the <code>Home</code> page and then come back to <code>Chat</code>, the connection doesn't work. What could be the reason?</p>
  
  <p>This is the code for navigating back to the <code>Home</code> page:</p>
  
  <pre><code>onPressed: () {
                  socket.destroy();
                  Navigator.pushAndRemoveUntil(
                      context,
                      MaterialPageRoute(
                        builder: (context) =&gt; MyHomePage(),
                      ),
                      (x) =&gt; false);
                },
  </code></pre>
  
  <p>No action is done on socket inside the <code>dispose()</code> method of the widget.</p>