title: Handling exception HTTP request flutter
body: <p>I want to handle http request flutter with some error message but I got so many errors here. I just make it based on the suggestion but it didn't work for me. Please, anyone, help me
  Here is my function to call API</p>
  <pre><code>getData(data, apiUrl) async {
      var tempUrl = _url + apiUrl + await _getToken();
      Uri uri = Uri.parse(tempUrl);
      var fullUrl = uri.replace(queryParameters: data);
      var res;
      try {
        var response = await http.get(fullUrl, headers: _setHeaders()).timeout(
            const Duration(seconds: 60));
        print(response.statusCode);
        if (response.statusCode != 200) {
          res = {
            &quot;success&quot;: false,
            &quot;status&quot;: response.statusCode,
            &quot;message&quot;: _returnResponse(response)
          };
        }
        else {
          res = response;
        }
      }
      on SocketException {
        throw FetchDataException('No Internet connection');
      }
      on TimeoutException catch (e) {
        res = {
          &quot;success&quot;: false,
          &quot;status&quot;: response.statusCode,
          &quot;message&quot;: &quot;Connection timeout&quot;
        };
      } on Error catch (e) {
        print('Error: $e');
      }
  
      return res;
    }
  </code></pre>
  <p>This is my return response for the others except 200</p>
  <pre><code>dynamic _returnResponse(http.Response response) {
      switch (response.statusCode) {
        case 400:
          throw BadRequestException(response.body.toString());
        case 401:
        case 403:
          throw UnauthorisedException(response.body.toString());
        case 500:
        default:
          throw FetchDataException(
              'Error occured while Communication with Server with StatusCode : ${response
                  .statusCode}');
      }
    }
  </code></pre>
  <p>and here is my app_exception.dart I got from StackOverflow and other forums</p>
  <pre><code>class AppException implements Exception {
    final _message;
    final _prefix;
  
    AppException([this._message, this._prefix]);
  
    String toString() {
      return &quot;$_prefix$_message&quot;;
    }
  }
  
  class FetchDataException extends AppException {
    FetchDataException([String message])
        : super(message, &quot;Error During Communication: &quot;);
  }
  
  class BadRequestException extends AppException {
    BadRequestException([message]) : super(message, &quot;Invalid Request: &quot;);
  }
  
  class UnauthorisedException extends AppException {
    UnauthorisedException([message]) : super(message, &quot;Unauthorised: &quot;);
  }
  
  class InvalidInputException extends AppException {
    InvalidInputException([String message]) : super(message, &quot;Invalid Input: &quot;);
  }
  </code></pre>
  <p>I have tried so many suggestions but it didn't work at all</p>
  <p>I got this error</p>
  <blockquote>
  <p>Error: 'SocketException' isn't a type.
  on SocketException {
  ^^^^^^^^^^^^^^^</p>
  <p>Error: 'TimeoutException' isn't a type.
  on TimeoutException catch (e) {
  ^^^^^^^^^^^^^^^^</p>
  </blockquote>