Created
July 30, 2024 02:47
-
-
Save OmarYehiaDev/26aef84f7ef5b6896c3c827ff499ab21 to your computer and use it in GitHub Desktop.
Simple Extractor for `YouTube Video ID` **without** using `RegExp`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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