Listing by Attributes When Parsing Html in Dart & Flutter
409
You need to use the where
function to filter your items:
var selectElements = document
.getElementsByClassName('hello')
.where((element) => element.attributes['data-time'] == '9112020')
.map((item) => item.text)
.toList();
here is a GitHub gist for my full code: https://gist.github.com/ali2236/14a12460a7b8f857e9e5c02583fff91c
There also seems to be a bug with the HTML parser in my example which should not happen in the real world.
Author by
Gurkan T
Updated on December 25, 2022Comments
-
Gurkan T over 1 year
var response = await http.get('https://www....'); dom.Document document = parser.parse(response.body); var selectElements = document.getElementsByClassName('hello').map((item) => item.children).toList();
I can sort and list by classes thanks to the http library, but I don't know how to do it according to attributes. How can I list by attributes?
There is an HTML output as below. I want to list the ones whose attribute data-time is 9112020 and see the results for lorem ipsum3 and lorem ipsum4.
<tr class="hello" data-country="Germany" data-time="8112020">lorem ipsum1</tr> <tr class="hello" data-country="Turkey" data-time="8112020">lorem ipsum2</tr> <tr class="hello" data-country="Germany" data-time="9112020">lorem ipsum3</tr> <tr class="hello" data-country="Turkey" data-time="9112020">lorem ipsum4</tr>