idFromUrl static method
Retrieve a comment ID from a given URL.
Note: when url
is a String, it must end with a trailing '/'. This is a
bug and will be fixed eventually.
Implementation
static String idFromUrl(/*String, Uri*/ url) {
Uri uri;
if (url is String) {
uri = Uri.parse(url);
} else if (url is Uri) {
uri = url;
} else {
throw DRAWArgumentError('idFromUrl expects either a String or Uri as'
' input');
}
final parts = uri.path.split('/');
final commentsIndex = parts.indexOf('comments');
// Check formatting of the URL.
if (commentsIndex != parts.length - 5) {
throw DRAWArgumentError("'$url' is not a valid comment url.");
}
return parts[parts.length - 2];
}