Introduction
If we want to visit a website, we need to know the address of this website. The address of the website is generally called URL, and its full name is Uniform Resource Locator. So what is a URI?
The whole URI is Uniform Resource Identifier, also called uniform resource identifier.
URI is used to mark resources, and URL is to mark resources on the network, so URL is a subset of URI.
After understanding the relationship between URI and URL, let's take a look at the URI support of the dart language.
URI in dart
A special class for URIs is created in dart called Uri:
abstract class Uri
Uri is an abstract class that defines some basic operations on URI. It has three implementation classes, namely _Uri, _DataUri and _SimpleUri.
Next, let's take a look at what Uri in dart can do.
encode and decode
Why encode URI?
Generally speaking, URI can contain some special characters, such as spaces or Chinese, etc. These characters may not be recognized by the other party during transmission. So we need to encode Uri.
But for some special but meaningful characters in URI, such as: /, :, &, #, these do not need to be escaped.
So we need a method that can unify encoding and decoding.
In dart, this method is called encodeFull() and decodeFull():
var uri = 'http://www.flydean.com/doc?title=dart uri';
var encoded = Uri.encodeFull(uri);
assert(encoded ==
'http://www.flydean.com/doc?title=dart%20uri');
var decoded = Uri.decodeFull(encoded);
assert(uri == decoded);
If you want to encode all characters, including those meaningful characters: /, :, &, #, then you can use encodeComponent() and decodeComponent():
var uri = 'http://www.flydean.com/doc?title=dart uri';
var encoded = Uri.encodeComponent(uri);
assert(encoded ==
'http%3A%2F%2www.flydean.com%2Fdoc%3Ftitle%3Ddart%20uri');
var decoded = Uri.decodeComponent(encoded);
assert(uri == decoded);
Resolve URI
URI is composed of scheme, host, path, fragment. We can decompose Uri through these attributes in Uri:
var uri =
Uri.parse('http://www.flydean.com/doc#dart');
assert(uri.scheme == 'http');
assert(uri.host == 'www.flydean.com');
assert(uri.path == '/doc');
assert(uri.fragment == 'dart');
assert(uri.origin == 'http://www.flydean.com');
So how to construct Uri? We can use Uri's constructor:
var uri = Uri(
scheme: 'http',
host: 'www.flydean.com',
path: '/doc',
fragment: 'dart');
assert(
uri.toString() == 'http://www.flydean.com/doc#dart');
Summarize
Dart provides us with a very simple Uri tool.
This article has been included in www.flydean.com
The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to discover!
Welcome to pay attention to my official account: "programs, those things", know the technology, know you better!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。