Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Dart throws ArgumentError 'No host specified in URI' when you make a request

ErrorInvalid argument(s): No host specified in URI example.com/api/users

Short answer

Dart's HttpClient throws this ArgumentError when the Uri it was given has an empty host. In practice the string had no scheme, the base URL was empty, or host:port was parsed as a scheme. Print the Uri, check uri.host, and build it with Uri.https or Uri.http.

  • Applies to Dart 3.x, Flutter 3.3x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the Uri exactly as the client receives it and look at its scheme and host. An empty host is what the check in dart:io rejects, and the URI in the error message is the one that failed.

    final uri = Uri.parse(url);
    print('scheme="${uri.scheme}" host="${uri.host}" path="${uri.path}"');
  2. 2Match what you see, then jump to its fix

    1. 1The URI in the message starts with the domain, for example example.com/api/usersThe URL string has no schemeFix
    2. 2Unsupported scheme 'localhost' in URI localhost:3000/api (or the same error with an IP address)host:port without a scheme is read as a schemeFix
    3. 3The URI in the message is only a path such as /api/users, or is emptyThe base URL is empty or null at runtimeFix

What does the error look like?

  • Invalid argument(s): No host specified in URI example.com/api/users
  • ArgumentError: No host specified in URI
  • Unsupported scheme 'localhost' in URI localhost:3000/api

Cause 1 of 3

The URL string has no scheme

1Check

A URI's authority (the host) is introduced by a double slash, so a string without https:// has no host at all; the whole text lands in the path. dart:io then rejects it because uri.host is empty.

final uri = Uri.parse('example.com/api/users');
print(uri.host.isEmpty); // true
print(uri.path);         // example.com/api/users

2Fix

Add the scheme, or build the Uri from its parts so the host cannot be forgotten.

final uri = Uri.https('example.com', '/api/users', {'limit': '10'});
// https://example.com/api/users?limit=10

3Verify

Print uri.host and send the request again.

print(uri.host); // example.com
final response = await HttpClient().getUrl(uri).then((r) => r.close());
print(response.statusCode);

Confirmed by api.dart.dev

Did this fix it?

Cause 2 of 3

host:port without a scheme is read as a scheme

1Check

In a URI reference, text before the first colon is read as a scheme name. So 'localhost:3000/api' is parsed as scheme localhost with path 3000/api, and the host is still empty.

final uri = Uri.parse('localhost:3000/api');
print(uri.scheme); // localhost
print(uri.host.isEmpty);

2Fix

Use Uri.http for plain HTTP development servers and pass the port inside the authority. On an Android emulator the computer's localhost is 10.0.2.2, not localhost.

final uri = Uri.http('10.0.2.2:3000', '/api');
// http://10.0.2.2:3000/api

3Verify

The printed scheme is http and the host is not empty.

print('${uri.scheme} ${uri.host} ${uri.port}');

Confirmed by rfc-editor.org

Did this fix it?

Cause 3 of 3

The base URL is empty or null at runtime

1Check

The host check runs on the final Uri, so an empty base URL joined to a path gives a URI with a path and no host. Typical sources are a config value that was not loaded yet, an empty environment variable, or a string interpolation of a missing value.

print('base="$baseUrl"');
print(Uri.parse('$baseUrl/api/users').host.isEmpty);

2Fix

Validate the base URL once at start-up and fail with a clear message there, instead of letting the HTTP client report it later.

final base = Uri.parse(baseUrl);
if (base.host.isEmpty) {
  throw StateError('API base URL has no host: "$baseUrl"');
}
final uri = base.replace(path: '/api/users');

3Verify

Run the app with the config loaded and confirm the request URI starts with https:// and a host.

print(uri);

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Build request URIs with Uri.https or Uri.http and keep the host in one constant instead of joining strings.
  • Check base URLs for an empty host when the app starts, so a missing config value fails early.
  • Never put a host:port pair into Uri.parse without a scheme in front of it.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.