Understanding the Uri class in Dart (from examples)

Suragch
3 min readDec 31, 2020
How do I parse out the individual pieces of a URL?

The Uri class in Dart has good documentation, but sometimes seeing examples makes it a lot easier to understand. This article will show examples for all of the main parameters to make them more clear.

Creating a Uri

You can create a Uri by parsing string like this:

Uri uri = Uri.parse('http://www.example.com');

The following examples will all reference uri but I’ll omit the line above to save space. Every example will start with the URL string to be parsed.

Properties of Uri

scheme

The scheme is something like http, https, or file. There are a lot more.

http://www.example.comString scheme = uri.scheme;
print(scheme); // http

authority

The authority in a URL is normally just the main website without the scheme or path or anything.

http://www.example.com/bananasString authority = uri.authority;
print(authority); // www.example.com

However, note that the authority can also include user info and port:

http://user:password@www.example.com:8080/bananas

--

--