How to prevent flutter app from crashing after Dio error?

Viewed 1267

I am using the flutter-dio package to make HTTP requests and have made a wrapper on it from my convenience like so

  Future<dynamic> post(String uri, dynamic body, {String authKeyCust}) async {
    authKey = await SecureStorage.getAuthKey();
    try {
      Response response = await dio.post(
        uri,
        data: body,
        options: Options(headers: {"authkey": authKeyCust ?? "$authKey"}),
      );
      return response.data;
    } on DioError catch (error) {
      print('error: $error');
    } on SocketException catch(error) {
      print('No net');
    }
  }

All I want is the app to not crash/pause on exception when any error occurs.

1 Answers

I also had the same problem.

First, try to run it from command prompt with flutter run see if there is occurring any problem or not.

If the command prompt is showing no problem & your app is running smoothly then you have to check the IDE. If you are using VSCode then switch to Debug is side bar, see which options are ticked in Breakpoint section. If All Exceptions is ticked then the debugger will pause on every exception. Uncheck both All Exceptions & Uncaught Exceptions then try refresh restart.

Hope this will solve your problem.

enter image description here

Related