Dart Uri fragment
Syntax & Examples


Uri.fragment property

The `fragment` property in Dart's Uri class returns the fragment identifier component of the URI.


Syntax of Uri.fragment

The syntax of Uri.fragment property is:

 String fragment 

This fragment property of Uri returns the fragment identifier component.

Return Type

Uri.fragment returns value of type String.



✐ Examples

1 Getting the fragment from a URI

In this example,

  1. We parse a URI with different fragment identifiers.
  2. We access the `fragment` property of the URI.
  3. We print the fragment to standard output.

Dart Program

void main() {
  Uri uri = Uri.parse('https://example.com/page#section1');
  print(uri.fragment);
}

Output

section1

2 Getting another fragment from a URI

In this example,

  1. We parse a URI with a different fragment identifier.
  2. We access the `fragment` property of the URI.
  3. We print the fragment to standard output.

Dart Program

void main() {
  Uri uri = Uri.parse('https://example.com/page#section2');
  print(uri.fragment);
}

Output

section2

3 Getting yet another fragment from a URI

In this example,

  1. We parse a URI with another fragment identifier.
  2. We access the `fragment` property of the URI.
  3. We print the fragment to standard output.

Dart Program

void main() {
  Uri uri = Uri.parse('https://example.com/page#section3');
  print(uri.fragment);
}

Output

section3

Summary

In this Dart tutorial, we learned about fragment property of Uri: the syntax and few working examples with output and detailed explanation for each example.