Skip to content

feat: add isOk convenience getter to BaseResponse#1874

Open
Turskyi wants to merge 3 commits intodart-lang:masterfrom
Turskyi:patch-1
Open

feat: add isOk convenience getter to BaseResponse#1874
Turskyi wants to merge 3 commits intodart-lang:masterfrom
Turskyi:patch-1

Conversation

@Turskyi
Copy link

@Turskyi Turskyi commented Jan 31, 2026

This PR adds a convenience getter isOk to the BaseResponse class.

What is changing? I have added a boolean getter isOk to BaseResponse that returns true if the statusCode is exactly 200. By placing it in the base class, this convenience is now available for all response types, including Response and StreamedResponse.
I have also added unit tests to verify this behavior across both response types and various status codes (200, 201, 404, 500).

Why? Checking for a 200 OK response is the most frequent operation when handling HTTP requests. Currently, developers must either:

  1. Use magic numbers (e.g., if (response.statusCode == 200)), which is less expressive.
  2. Import dart:io to use HttpStatus.ok (e.g., if (response.statusCode == HttpStatus.ok)).

This addition makes the API more intuitive and readable while remaining platform-agnostic:

final response = await http.get(url);
if (response.isOk) {
  // Handle success
}


[x] I’ve reviewed the contributor guide and applied the relevant portions to this PR.

Contribution guidelines:
• See our contributor guide for general expectations for PRs. • Larger or significant changes should be discussed in an issue before creating a PR. • Contributions to our repos should follow the Dart style guide and use dart format. • Most changes should add an entry to the changelog and may need to rev the pubspec package version. • Changes to packages require corresponding tests. Many Dart repos have a weekly cadence for reviewing PRs - please allow for some latency before initial review feedback.

Turskyi and others added 2 commits January 31, 2026 14:38
Introduces a boolean getter `isOk` to the `Response` class that returns true 
if the `statusCode` is 200.

This provides a more expressive and readable way for developers to verify 
successful responses, reducing the need for magic numbers or platform-specific 
imports (like dart:io's HttpStatus) in client code.
Add unit tests to verify the behavior of the `isOk` getter on the `Response` class, ensuring it returns `true` only for status code 200.
* Move the `isOk` property from `Response` to `BaseResponse` so it is available to all response types (e.g. `StreamedResponse`).
* Update tests to verify `isOk` behavior on both `Response` and `StreamedResponse`.
@Turskyi Turskyi changed the title feat: Add isOk convenience getter to Response feat: add isOk convenience getter to BaseResponse Jan 31, 2026
@Reprevise
Copy link
Contributor

Personally, I'm more in favor of something like an isSuccess that checks for >=200 && <400

@Turskyi
Copy link
Author

Turskyi commented Feb 4, 2026

Personally, I'm more in favor of something like an isSuccess that checks for >=200 && <400

Thanks for the suggestion - that makes sense as a broader convenience 👍

Right now this PR adds isOk specifically for statusCode == 200. The intention here was:
• to keep the change small and focused,
• and to match the most common check people do when they care about exactly 200.

A broader isSuccess getter (e.g. statusCode >= 200 && statusCode < 400) could definitely be useful, but that’s a slightly different API/design choice. I think it might deserve its own discussion or a follow-up PR if maintainers want that.

Happy to update this PR based on maintainer feedback!

@github-actions
Copy link

github-actions bot commented Feb 6, 2026

PR Health

License Headers ✔️
// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
Files
no missing headers

All source files should start with a license header.

Unrelated files missing license headers
Files
pkgs/http/example/main.dart
pkgs/http_multi_server/test/cert.dart

This check can be disabled by tagging the PR with skip-license-check.

API leaks ✔️

The following packages contain symbols visible in the public API, but not exported by the library. Export these symbols or remove them from your publicly visible API.

Package Leaked API symbol Leaking sources

This check can be disabled by tagging the PR with skip-leaking-check.

Breaking changes ✔️
Package Change Current Version New Version Needed Version Looking good?
http Non-Breaking 1.6.0 1.6.1-wip 1.6.1-wip ✔️

This check can be disabled by tagging the PR with skip-breaking-check.

Unused Dependencies ⚠️
Package Status
http
❗ Show Issues
These packages may be unused, or you may be using assets from these packages:
* dart_flutter_team_lints
* shelf

For details on how to fix these, see dependency_validator.

This check can be disabled by tagging the PR with skip-unused-dependencies-check.

Coverage ✔️
File Coverage
pkgs/http/lib/src/base_response.dart 💚 88 % ⬆️ 1 %

This check for test coverage is informational (issues shown here will not fail the PR).

This check can be disabled by tagging the PR with skip-coverage-check.

Changelog Entry
Package Changed Files
package:http pkgs/http/lib/src/base_response.dart

Changes to files need to be accounted for in their respective changelogs.

This check can be disabled by tagging the PR with skip-changelog-check.

@brianquinlan
Copy link
Collaborator

I don't think that we can merge this as-is because there could be instances of implements BaseResponse, which would be broken by this implementation.

Instead, we could implement this as an extension method.

I'd suggest one check per response type, e.g. isSuccessful, and having isOk for exactly 200.

WDYT?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants