Skip to content

[flutter web] violently crashes app on when the server isn't available #15

Description

@lukepighetti

Description

If you try to connect to a websocket server that isn't available on flutter web the app violently crashes even if you try to catch the error.

flutter --version

Flutter 2.1.0-12.1.pre • channel dev • https://github.com/flutter/flutter.git
Framework • revision 8264cb3e8a (12 days ago) • 2021-03-10 12:37:57 -0800
Engine • revision 711ab3fda0
Tools • Dart 2.13.0 (build 2.13.0-116.0.dev)

Reproduction

  • run MCVE on Chrome (web environment) with no server available at ws://localhost:8000/foo

Expected Results

  • Websocket tries to connect, fails
  • The try/catch block successfully catches the error
  • App to proceeds to execute as expected

Actual Results

  • Websocket tries to connect, fails
  • The try/catch block fails to catch the error
  • App violently crashes

Error State

Screen Shot 2021-03-22 at 11 40 10 AM

MCVE

import 'package:flutter/material.dart';
import 'package:socketcluster_client/socketcluster_client.dart';

void main() async {
  try {
    await Socket.connect('ws://localhost:8000/foo', listener: MyListener());
  } catch (e) {
    print('caught $e');
  } finally {
    runApp(MyApp());
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Websocket Issue',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        body: Center(
          child: Text('Websocket Issue'),
        ),
      ),
    );
  }
}

class MyListener extends BasicListener {
  @override
  void onAuthentication(Socket socket, bool status) {
    print('onAuthentication');
  }

  @override
  void onConnectError(Socket socket, e) {
    print('onConnectError');
  }

  @override
  void onConnected(Socket socket) {
    print('onConnected');
  }

  @override
  void onDisconnected(Socket socket) {
    print('onDisconnected');
  }

  @override
  void onSetAuthToken(String token, Socket socket) {
    print('onSetAuthToken');
  }
}
name: flutter_websocket_issue
description: A new Flutter project.
publish_to: "none"
version: 1.0.0+1

environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.2
  socketcluster_client: ^0.3.0

dev_dependencies:
  flutter_test:
    sdk: flutter

flutter:
  uses-material-design: true

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions