'How to use WebSocketChannelException in Flutter

i'm experiencing an issue with flutter. The connection to websocket works well but id like to show a ToastNotification onError

So for that i'm trying an aproach with "try catch" but doesn't work that way it seems.

 try {
  print("Connecting to ${WebSocketConfig.getUrl()}");
  IOWebSocketChannel channel = IOWebSocketChannel.connect(WebSocketConfig.getUrl());

  if (channel != null) {
    print("Websocket connected to ${WebSocketConfig.getUrl()}");
  }
  return channel;

} catch(e) {
  Fluttertoast.showToast(
      msg: e,
      toastLength: Toast.LENGTH_SHORT,
      gravity: ToastGravity.CENTER,
      timeInSecForIosWeb: 1,
      backgroundColor: Colors.red,
      textColor: Colors.white,
      fontSize: 16.0);
}

But it doesn't seem to work that way.

I would like to show a toast message when there is an error on websocket connection.



Sources

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

Source: Stack Overflow

Solution Source