How to parse a div content with Flutter?

Issue

I have the following DOM structure:

<td title="Gardien" class="zentriert rueckennummer bg_Torwart">
  <div class="rn_nummer">30</div>
</td>

I want to get the content of the div tag (which is 30 here) using Dart & Flutter…

I tested the following code:

List<String> numeros = [];

final numeross = document.getElementsByTagName("div");
numeros = numeross.map
 ((e) => e.getElementsByClassName("rn_nummer")[0].innerHtml).toList();

But, I always got an error

E/flutter ( 6923): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: RangeError (index): Invalid value: Valid value range is empty: 0

I searched a lot but in vain…
Any suggestion? thx

Solution

try with this and here I am using html package

  var document = parse("""
    <td title="Gardien" class="zentriert rueckennummer bg_Torwart">
        <div class="rn_nummer">30</div>
        <div class="rn_nummer">30</div>
        <div class="rn_nummer">30</div>
     </td>
  """);



  //declaring a list of String to hold all the data.
  List<String> data = document.getElementsByClassName("rn_nummer").map((e) => e.innerHtml).toList();

 print(data);

output: [30,30,30]

Answered By – Jahidul Islam

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published