flutter / website

Flutter documentation web site
https://docs.flutter.dev
Other
2.8k stars 3.21k forks source link

Explain how to catch errors on 'Work with WebSockets' page #8765

Open JadKHaddad opened 1 year ago

JadKHaddad commented 1 year ago

Page URL

https://docs.flutter.dev/cookbook/networking/web-sockets/

Page source

https://github.com/flutter/website/tree/main/src/cookbook/networking/web-sockets.md

Describe the problem

Connection errors while establishing a connection with WebSocketChannel.connect() can't be handeled with try/catch blocks.

Expected fix

No response

Additional context

Establishing connections to a none available server causes my app to crash even though I am trying to catch the error in a try/catch block

JadKHaddad commented 1 year ago

Tracking progress on dart-lang/web_socket_channel#268

JadKHaddad commented 1 year ago
WebSocketChannel channel = WebSocketChannel.connect(
    Uri.parse('ws://localhost:8000/ws'),
);

try {
    await channel.ready;
} catch (e) {
    print("Exception: $e");
}