I have a problem when I want to parse the response from the server as shown below, can anyone help me to solve this problem
How to retrieve URL on third line of response as drawn, using flutter?
#EXTM3U #EXTINF:0 tvg-logo="http://example.com/logo/" tvg-id="" ,METRO TV http://127.0.0.1:8000/live/test/test/3
CodePudding user response:
First get response in the String
data type.
Now you can do it by extract all urls from whole string response to the list and get last item from that list.
Get list of URLs:
final urlRegExp = new RegExp(
r"((https?:www\.)|(https?:\/\/)|(www\.))[-a-zA-Z0-9@:%._\ ~#=]{1,256}\.[a-zA-Z0-9]{1,6}(\/[-a-zA-Z0-9()@:%_\ .~#?&\/=]*)?");
final urlMatches = urlRegExp.allMatches(response);
List<String> urls = urlMatches.map(
(urlMatch) => text.substring(urlMatch.start, urlMatch.end))
.toList();
Now get last element from the list:
print(urls[urls.length-1]);
CodePudding user response:
You can achieve this by using the split method offered by the LineSplittere class which returns a list of lines.
Code:
import 'dart:convert';
void main() {
String text = '''#EXTM3U
#EXTINF:0 tvg-logo="http://example.com/logo/" tvg-id="", METRO TV
http://127.0.0.1:8000/live/test/test/3''';
print(LineSplitter.split(text).toList()[2]);
}
Output:
http://127.0.0.1:8000/live/test/test/3