0

Why is it cannot refer String tit, kit?? after elementAt() instance has MANUAL01..MANUAL02

I want refer last element on next code.

snapshot.data!.COOKRCP02.row.elementAt(index).MANUAL01 <-- this

code

manualList(snapshot, index) {
  {
    for (int i = 1; i < 20; i++) {
    String tik = 'MANUAL'+i.toString(); // it says unused_local_variable
    String kik = 'MANUALIMG'+i.toString(); // it says unused_local_variable
    if(i<10){
      tik = 'MANUAL0'+i.toString();
      kik = 'MANUALIMG0'+i.toString();
    }
    var a = snapshot.data!.COOKRCP02.row.elementAt(index).tik; //<--
    var b = snapshot.data!.COOKRCP02.row.elementAt(index).kik; //<--
      if (a != null && b != null) {
        return Column(
          children: [
            Text(a),
            Image.network(b),
          ],
        );
      } else if (a != null && b == null) {
        return Column(
          children: [
            Text(a),
          ],
        );
      }
    }
  }
}

error

error message

Row Class

enter image description here

Hajime
  • 212
  • 3
  • 13
  • 1
    You're trying to dynamically access `snapshot.data!.COOKRCP02.row.elementAt(index).MANUAL01`, but when do `snapshot.data!.COOKRCP02.row.elementAt(index).tik` it's trying to access the property `tik`, not the variable `tik`. You'll need to look at the documentation to determine how to access a property using a string, such as `.elementAt(index).get("MANUAL01")`, and if that works, then you can do `.elementAt(index).get(tik)` – WOUNDEDStevenJones May 03 '22 at 16:53
  • thanks for your answer, .get() does not work, should I add get() method in class Row? – Hajime May 03 '22 at 17:11
  • 1
    I need to look https://stackoverflow.com/questions/55445624/how-to-get-a-property-by-this-name-in-string thanks for your answer, sincerely – Hajime May 03 '22 at 17:13
  • 1
    https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable does `.elementAt(index)["MANUAL01"]` work? – WOUNDEDStevenJones May 03 '22 at 17:13
  • unfortunately, it does not work. same results – Hajime May 03 '22 at 17:15
  • 1
    Can you debug this further? What does `snapshot.data!.COOKRCP02.row.elementAt(index)` output or show in the debugger? Can you confirm that it even has properties `MANUAL01` - `MANUAL19` (FYI your loop won't check `MANUAL20` because you have `i < 20`. If you want to include 20, change this to `<=`)? – WOUNDEDStevenJones May 03 '22 at 18:40
  • yep it has properties. output is good when using with toString(). but I need to access properties.. it is not easy as I thought – Hajime May 04 '22 at 14:27
  • trying add get method on my class for now. (https://stackoverflow.com/questions/60853932/flutter-dart-dynamically-get-a-property-of-a-class) btw thanks for keep pay attention. – Hajime May 04 '22 at 14:31

0 Answers0