Skip to main content

avoid-incorrect-uri

added in: 1.24.0
Pro+

Warns when the Uri constructor receives incorrect arguments.

The Uri class is quite old and comes from the Dart 1 days. For backward compatibility, some parameters do not have strong types and are only check at runtime. Passing values with not supported types will throw an exception.

Example

❌ Bad:

// LINT: This expression will throw at runtime because query params are expected to be either 'String' or 'List<String>'.
Uri(queryParameters: {'hi', 1});

// LINT: This expression will throw at runtime because query params are expected to be either 'String' or 'List<String>'.
Uri.https('', '', {'hi': 1});

// LINT: This expression will throw at runtime because query params are expected to be either 'String' or 'List<String>'.
Uri.http('', '', {'hi': 1});

// LINT: Avoid creating Uri with both 'path' and 'pathSegments' as it will throw at runtime.
Uri(path: '', pathSegments: ['']);

✅ Good:

Uri(fragment: '', queryParameters: {'hi': 'hello'});
Uri.https('', '', {'hi': 'hello'});
Uri(path: '');