Home > Net >  How to detect change in the URL - Dart/Flutter
How to detect change in the URL - Dart/Flutter

Time:04-30

I'm trying to fetch a specific part from the youtube URL. For example:- if the user has put the following URL https://www.youtube.com/watch?v=KOjE7cQ0FkA

I would fetch the following part KOjE7cQ0FkA using the below method

          String url = urlController.text; // https://www.youtube.com/watch?v=KOjE7cQ0FkA
          List urlList = url.split("=");
          String urlCode = await urlList[1];

but sometimes people copy the video link from mobile and this is how it would appear https://youtu.be/KOjE7cQ0FkA In such case, above code wouldn't work

So how can I detect which URL is put by the user and perform a split operation accordingly

Sorry, if my question sounds stupid but I hope you got an idea that what I'm trying to achieve here

CodePudding user response:

There are many ways to approach this. One way would be to parse the URL (have a look at this SO question).

With this approach you could check if the query parameter watch is present -- if not the URL is probably in the mobile format.


Another approach would be to define a regex expression for the Video-ID (KOjE7cQ0FkA) part of the URL. That way you can extract the Video-ID regardless of the format of the URL. I would probably go with that approach.

Your regex could look like this: ([a-zA-Z] (\d[a-zA-Z] ) )

I used this site to create the regex. You probably need to modify it a little bit. Also if the ID has a fixed length that is a great criteria to filter by.

  • Related