2 thoughts on “Try catch after upgrade flutter no catch any exception in http package

  1. @AlaaBqshool
    everything seems to work just fine,
    try this code sample

    code sample
    import 'package:http/http.dart' as http;
    import 'package:flutter/material.dart';
    
    abstract class HttpResult {
      const factory HttpResult.good(String body) = Good;
      const factory HttpResult.bad(int statusCode) = Bad;
      const factory HttpResult.ugly(Object e, StackTrace? s) = Ugly;
    }
    
    class Good implements HttpResult {
      final String body;
      const Good(this.body);
    }
    
    class Bad implements HttpResult {
      final int responseCode;
      const Bad(this.responseCode);
    }
    
    class Ugly implements HttpResult {
      final Object e;
      final StackTrace? s;
      const Ugly(this.e, this.s);
    }
    
    Uri? _badUri;
    
    Future<HttpResult> throwingAPI() async {
      await Future.delayed(const Duration(seconds: 2));
      try {
        final res = await http.get(_badUri!);
        return res.statusCode == 200
            ? HttpResult.good(res.body)
            : HttpResult.bad(res.statusCode);
      } on Error catch (e) {
        return HttpResult.ugly(e, e.stackTrace);
      } on Exception catch (e, s) {
        return HttpResult.ugly(e, s);
      }
    }
    
    void main() => runApp(
          MaterialApp(
            home: Material(
              child: Center(
                child: FutureBuilder<HttpResult>(
                  future: throwingAPI(),
                  builder: (context, future) => !future.hasData
                      ? const CircularProgressIndicator()
                      : future.hasError
                          ? const Icon(Icons.error)
                          : future.data is Ugly
                              ? Text('${(future.data as Ugly).e}')
                              : future.data is Good
                                  ? Text((future.data as Good).body)
                                  : Text('${(future.data as Bad).responseCode}'),
                ),
              ),
            ),
          ),
        );
    pubspec
    environment:
      sdk: ">=2.12.0 <3.0.0"
    
    dependencies:
      flutter:
        sdk: flutter
      http: ^0.13.0
    
    flutter:
      uses-material-design: true
    doctor
    [✓] Flutter (Channel master, 2.1.0-11.0.pre.165, on Linux, locale en_US.UTF-8)
        • Flutter version 2.1.0-11.0.pre.165 at /home/francesco/snap/flutter/common/flutter
        • Framework revision 698d438960 (15 hours ago), 2021-03-09 07:53:21 +0100
        • Engine revision 2441c476a6
        • Dart version 2.13.0 (build 2.13.0-116.0.dev)
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
        • Android SDK at /home/francesco/Android/Sdk
        • Platform android-30, build-tools 30.0.2
        • ANDROID_SDK_ROOT = /home/francesco/Android/Sdk
        • Java binary at: /usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/bin/java
        • Java version OpenJDK Runtime Environment (build 1.8.0_282-8u282-b08-0ubuntu1~20.04-b08)
        • All Android licenses accepted.
    
    [✓] Chrome - develop for the web
        • CHROME_EXECUTABLE = /snap/bin/chromium
    
    [✓] Linux toolchain - develop for Linux desktop
        • clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
        • cmake version 3.10.2
        • ninja version 1.8.2
        • pkg-config version 0.29.1
    
    [!] Android Studio (not installed)
        • Android Studio not found; download from https://developer.android.com/studio/index.html
          (or visit https://flutter.dev/docs/get-started/install/linux#android-setup for detailed instructions).
    
    [✓] Connected device (2 available)
        • Linux (desktop) • linux  • linux-x64      • Linux
        • Chrome (web)    • chrome • web-javascript • Chromium 89.0.4389.82 snap
    
    ! Doctor found issues in 1 category.
  2. The problem was due to vs code so i apologize to all of you and flutter. Where the vs code is the exceptions from the http library are uncaught exceptions … where I have activated the uncaught exceptions option under breakpoints ,