Skip to content

docs(readme): add close method override in UserAgentClient example#1949

Open
EchoEllet wants to merge 1 commit into
dart-lang:masterfrom
EchoEllet:patch-2
Open

docs(readme): add close method override in UserAgentClient example#1949
EchoEllet wants to merge 1 commit into
dart-lang:masterfrom
EchoEllet:patch-2

Conversation

@EchoEllet

@EchoEllet EchoEllet commented Jul 5, 2026

Copy link
Copy Markdown

It seems that close() is a no-op without an override in this case:

import 'package:http/http.dart' as http;
import 'package:http/io.dart';

// Copied from https://pub.dev/packages/http#using
class UserAgentClient(final String userAgent, final http.Client _inner) extends http.BaseClient {

  @override
  Future<http.StreamedResponse> send(http.BaseRequest request) {
    request.headers['user-agent'] = userAgent;
    return _inner.send(request);
  }
}

void main() {
  final http.BaseClient client = UserAgentClient('user-agent', IOClient());
  client.close(); // No-op. Does not call IOClient.close()
}

I'm not sure if I misunderstood something or am consuming the API in the wrong way.

If the intended way to close the HTTP client is:

void main() {
  final http.BaseClient innerClient = IOClient();
  
  final http.BaseClient client = UserAgentClient('user-agent', innerClient);
   // Send requests using "client"...

  innerClient.close();
}

Then the current example code is as intended. But even then, I would throw a Dart error when calling UserAgentClient.close(), to avoid silent programming bugs.


  • I’ve reviewed the contributor guide and applied the relevant portions to this PR.
Contribution guidelines:

Many Dart repos have a weekly cadence for reviewing PRs - please allow for some latency before initial review feedback.

Note: The Dart team is trialing Gemini Code Assist. Don't take its comments as final Dart team feedback. Use the suggestions if they're helpful; otherwise, wait for a human reviewer.

@EchoEllet

Copy link
Copy Markdown
Author

If the intended way to close the HTTP client is:

It is worth noting that RetryClient (from the http package) calls _inner.close() (source). So I don't think the current example is as intended.

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.

1 participant