Josip Domazet
Josip Domazet

Reputation: 2890

Flutter/Dart: Easy way to check if http request was successful?

While using Flutters http request library I was wondering if there was an easy way to check if a request was successful without having to check the status code.

Most code samples I saw (including official ones) do something like this:

http.Response response = await http.Client().get(...);
    
if (response.statusCode == 200) {
   ...
}

However, those examples are ignoring the fact that all HTTP status codes starting with 2XX are considered successful. For example 201 stands for 201 Created.

I know for a fact that pythons http library has a simple ok flag, so in python could do something like this:

if response.ok:
   ...

Is there a feature like this in flutter/dart or do I have to implement a manual check?

Upvotes: 6

Views: 7213

Answers (2)

Andres Garcia
Andres Garcia

Reputation: 36

You can easily check if an HTTP request was successful in Flutter/Dart using the http_status package. Here's how you can do it:

  1. Classic method

    import 'package:http/http.dart' as http;
    import 'package:http_status/http_status.dart';
    
    final res = await http.get(Uri.parse(url));
    
    if (res.statusCode == HttpStatusCode.ok) { // res.statusCode == 200
      final httpStatus = HttpStatus.fromCode(res.statusCode);
    
      return {
        'statusCode': res.statusCode,
        'httpStatus': httpStatus,
        'data': res.body
      };
    }
    
  2. Alternative method (Same as #1 method, but with more direct validation using .isSuccessfulHttpStatusCode)

    import 'package:http/http.dart' as http;
    import 'package:http_status/http_status.dart';
    
    final res = await http.get(Uri.parse(url));
    
    if (res.statusCode.isSuccessfulHttpStatusCode) { // Http Status Code 200 - 299
      final httpStatus = HttpStatus.fromCode(res.statusCode);
    
      return {
        'statusCode': res.statusCode,
        'httpStatus': httpStatus,
        'data': res.body
      };
    }
    
  3. Alternative method (Same as #1 method, if you need the HttpStatus object from the dynamically generated status code of the response)

    import 'package:http/http.dart' as http;
    import 'package:http_status/http_status.dart';
    
    final res = await http.get(Uri.parse(url));
    
    final httpStatusResponse = HttpStatus.fromCode(res.statusCode);
    
    if (httpStatusResponse.isSuccessfulHttpStatusCode) { // Http Status Code 200 - 299
      return {
        'statusCode': res.statusCode,
        'httpStatus': httpStatusResponse,
        'data': res.body
      };
    } else if (httpStatusResponse.isClientErrorHttpStatusCode) { // Http Status Code 400 - 499
       // Handle client error
       // ...
    } else {
       // Handle other code error
       // ...
    }
    

Upvotes: 0

Christopher Moore
Christopher Moore

Reputation: 17141

You can make an extension method that does this easily:

extension IsOk on http.Response {
  bool get ok {
    return (statusCode ~/ 100) == 2;
  }
}

By adding the extension above, you can use the same response response.ok syntax that you like.


This extension works by isolating the 100th place digit by doing a truncating division by 100 and comparing the result to 2.

Upvotes: 20

Related Questions