Freewind
Freewind

Reputation: 198338

Where is `dart:uri` now?

I want to call decodeUrl(...) and I was doing it as:

import "dart:uri";

main() {
    decodeUrl("str");
}

But now with the latest Dart-SDK, it reports:

Do not know how to load 'dart:uri'
import 'dart:uri';
^

Seems it has been removed from the SDK. I searched a lot but still don't know where is it now. How to use the decodeUrl(...) with latest SDK now?

Upvotes: 6

Views: 1077

Answers (2)

mezoni
mezoni

Reputation: 11220

From Dart announcements of new releases, breaking changes, and other important news.

Library dart:uri removed and class Uri added to core.

The library dart:uri has been removed.

What changed?

The library dart:uri has been removed.

The class Uri is now in the core library. The encodeUriComponent, enocdeUri, decodeUriComponent, decodeUri top level functions from dart:uri has been moved to static methods Uri.encodeComponent, Uri.enocdeFull, Uri.decodeComponent, Uri.decodeFull.

The constructor Uri.fromComponents has been renamed to just Uri and the previous Uri constructor taking a URI string is no longer available but have to be replaced with a call to the static method Uri.parse.

Finally the handling of space to plus and plus to space encodeing/decoding has been removed from Uri.encodeComponent and Uri.decodeComponent. To get this encoding/decoding use the added static methods Uri.encodeQueryComponent and Uri.decodeQueryComponent.

Besides this the Uri class have additional features. See the change and the dartdoc for more information.

The dartdoc for the Uri class will be improved in the next couple of days.

Who is affected?

Users of dart:uri.

How do I update my code?

Change the use of new Uri(...) to Uri.parse(...)

Change the use of new Uri.fromComponents(...) to new Uri(...)

Change calls of encodeUriComponent, enocdeUri, decodeUriComponent, decodeUri to calls to Uri.encodeComponent, Uri.enocdeFull, Uri.decodeComponent, Uri.decodeFull.

Finally check of you use the fact that encodeUriComponent and decodeUriComponent changed space to plus and plus to space. If so use Uri.encodeQueryComponent and Uri.decodeQueryComponent instead of Uri.encodeComponent and Uri.decodeComponent

Upvotes: 4

jrockway
jrockway

Reputation: 42684

According to this post to the Dart announcement list, dart:uri has been replaced with the core Uri class.

Your code translates to:

main() {
  decodeUrl("str");
}

Though it might be more instructive to write print(Uri.decodeFull("str%20here"));.

Upvotes: 7

Related Questions