Skip to main content

HttpStatusCode

The HttpStatusCode class provides a centralized collection of commonly used HTTP status codes, grouped by their respective categories: success, redirection, client error, and server error.

This makes it easier to avoid magic numbers, improve code readability, and reduce typos when working with HTTP responses.


✅ Usage Example

if (response.statusCode == HttpStatusCode.ok) {
print('Request succeeded!');
} else if (response.statusCode == HttpStatusCode.notFound) {
print('Resource not found.');
}

📚 Available Constants

✅ 2xx — Success

NameCodeDescription
ok200Standard success response
created201Resource successfully created
accepted202Request accepted for processing
noContent204Success with no response body

🔁 3xx — Redirection

NameCode
movedPermanently301
found302
seeOther303
notModified304
temporaryRedirect307
permanentRedirect308

🚫 4xx — Client Errors

NameCode
badRequest400
unauthorized401
paymentRequired402
forbidden403
notFound404
methodNotAllowed405
notAcceptable406
requestTimeout408
conflict409
gone410
unprocessableEntity422
tooManyRequests429

💥 5xx — Server Errors

NameCode
internalServerError500
notImplemented501
badGateway502
serviceUnavailable503
gatewayTimeout504

🛠 Best Practices

  • Use these constants in your API clients and error handlers to improve clarity.
  • Map status codes to custom exceptions or UI messages for better UX.

📂 Location

Defined in:
lib/constants/constants.dart

Make sure it's properly referenced via:

part 'http_status_code.dart';