Skip to content

Instantly share code, notes, and snippets.

@OmarYehiaDev
Created July 30, 2024 02:47
Show Gist options
  • Save OmarYehiaDev/26aef84f7ef5b6896c3c827ff499ab21 to your computer and use it in GitHub Desktop.
Save OmarYehiaDev/26aef84f7ef5b6896c3c827ff499ab21 to your computer and use it in GitHub Desktop.
Simple Extractor for `YouTube Video ID` **without** using `RegExp`.
void main() {
final id1 = getVideoIdFromUrl(
"https://www.youtube.com/watch?v=L2XfHREa0j0",
);
final id2 = getVideoIdFromUrl(
"https://youtu.be/wCRw3hgIZv4?si=nMn_fyLz-JFnFypU",
);
print(id1);
print("----------");
print(id2);
}
String getVideoIdFromUrl(String url) {
String id;
final uri = Uri.parse(url);
if (uri.host == "youtu.be") {
id = uri.pathSegments.first;
} else {
id = uri.queryParameters["v"]!;
}
return id;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment