Dart throws ArgumentError 'No host specified in URI' when you make a request
Invalid argument(s): No host specified in URI example.com/api/usersShort 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.
Fix it fast
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}"');2Match what you see, then jump to its fix
- 1
The URI in the message starts with the domain, for example example.com/api/usersThe URL string has no schemeFix - 2
Unsupported 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
The URI in the message is only a path such as /api/users, or is emptyThe base URL is empty or null at runtimeFix
- 1
What does the error look like?
Invalid argument(s): No host specified in URI example.com/api/usersArgumentError: No host specified in URIUnsupported 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/users2Fix
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=103Verify
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
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/api3Verify
The printed scheme is http and the host is not empty.
print('${uri.scheme} ${uri.host} ${uri.port}');Confirmed by rfc-editor.org
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
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Mobile apps
AudioCache has no play method after updating audioplayers
The method 'play' isn't defined for the type 'AudioCache'.
Since audioplayers 1.0, AudioCache only turns asset files into local files and caches them.
2 causes, each with a fixMobile apps
Flutter Bloc throws that emit was called after the event handler completed
emit was called after an event handler completed normally.
Each on<Event> handler gets its own Emitter, and it stops working the moment the handler's returned Future completes.
2 causes, each with a fixMobile apps
analysis_options.yaml: the URI it includes can't be found
include_file_not_found
The analyzer reports include_file_not_found when the file named after include: in analysis_options.yaml does not resolve.
2 causes, each with a fixMobile apps
Dart error: the body might complete normally and return null for a non-nullable type
body_might_complete_normally
Dart found a path through your function or callback that reaches the closing brace without a return.
3 causes, each with a fixMobile apps
Dart throws ConcurrentModificationError while looping over a list
ConcurrentModificationError
Dart throws this when a collection changes size while something iterates it.
2 causes, each with a fixMobile apps
Dart cannot turn a compact timestamp like 20201101153000 into a DateTime
FormatException: Invalid date format
DateTime.parse accepts a subset of ISO 8601, and a time part must be introduced by T or a space.
2 causes, each with a fix