Как вернуть виджет из цикла for с данными json во флаттере?

У меня возникла проблема с повторением цикла for для индекса json, и я ожидаю возврата виджета из цикла for. Я сохранил данные файла Json в переменной mydata, а json имеет 3 индекса. В индексе 0 17 вопросов и ответы в индексе 2 соответственно. Я хочу напечатать их на карточке в следующем порядке: Карточка 1: Вопрос 1: "Данные вопроса 1" Ответ: "Ответ на вопрос 1"

Карточка 2: Вопрос 2: "Данные вопроса 2" Ответ: "Ответ на вопрос 2"

Вот данные файла JSON:

[
{
    "1": "Conjugation is facilitated by",
    "2": "Bacterial endospores function in",
    "3": "Pasteur's main achievements are the development of vaccines for diseases",
    "4": "Germ theory of disease was formulated by",
    "5": "The major locomotory structures in bacteria are",
    "6": "Plasmid is",
    "7": "Which of the following is a primary bacterial cell wall function",
    "8": "When flagella surround the whole cell, the condition is called",
    "9": "Bacterial membrane also contain enzyme for",
    "10": "Which of the following is not found in all bacteria is",
    "11": "Greater pathogenicity to bacteria and protection against phagocytosis is provided by",
    "12": "Mesosomes are internal extensions of the",
    "13": "Bacterial membrane differs from eukaryotic membrane in",
    "14": "Which one of following class of bacteria has the smallest size",
    "15": "Which of the following is present in both gram-positive and gram-negative cell walls",
    "16": "Gram negaive cell wall has",
    "17": "One of the following has flagella rarely"
},
{
    "1": {
        "a": "Capsule",
        "b": "Pili",
        "c": "Flagella",
        "d": "Both pili and flagella"
    },
    "2": {
        "a": "Reproduction",
        "b": "Protein synthesis",
        "c": "Survival",
        "d": "Stronge"
    },
    "3": {
        "a": "Cholera, rabies only",
        "b": "Anthrax, rabies only",
        "c": "Anthrax, fowl cholera and rabies",
        "d": "None of the above"
    },
    "4": {
        "a": "Antone Van Leeuwenhoek",
        "b": "Pasteur",
        "c": "Robert Koch",
        "d": "none of above"
    },
    "5": {
        "a": "Flagella",
        "b": "Pili",
        "c": "Both a and b",
        "d": "None of these"
    },
    "6": {
        "a": "Essential for bacterial growth and metabolism",
        "b": "Drug resistant having disease and insect resistant gene",
        "c": "Essential for bacterial growth only",
        "d": "All of above"
    },
    "7": {
        "a": "Transport",
        "b": "Support",
        "c": "Motility",
        "d": "Adhesion"
    },
    "8": {
        "a": "Peritrichous",
        "b": "Atrichous",
        "c": "Amphitrichous",
        "d": "None of above"
    },
    "9": {
        "a": "Respiration",
        "b": "Photosynthesis",
        "c": "Protein synthesis",
        "d": "None of the above"
    },
    "10": {
        "a": "Cell membrane",
        "b": "A nucleoid",
        "c": "Flygella",
        "d": "None of these"
    },
    "11": {
        "a": "Capsule",
        "b": "Slime",
        "c": "Cell wall",
        "d": "Mesosomes"
    },
    "12": {
        "a": "Cell wall",
        "b": "Cell membrane",
        "c": "Chromatin body",
        "d": "Capsule"
    },
    "13": {
        "a": "Lacking protein",
        "b": "Lacking lipids",
        "c": "Lacking polysaccharides",
        "d": "Lacking sterol i.e. cholesterol"
    },
    "14": {
        "a": "Bacillus subtilis",
        "b": "Mycoplasma",
        "c": "E-coli",
        "d": "Straptococci"
    },
    "15": {
        "a": "An outer membrane",
        "b": "Peptidoglycan",
        "c": "Techoic acid",
        "d": "Lipopolysaccharides"
    },
    "16": {
        "a": "Only lipids",
        "b": "Only protein",
        "c": "More lipids and less protein",
        "d": "Less lipids and more protein"
    },
    "17": {
        "a": "Diplobacilli",
        "b": "Spiral",
        "c": "Cocci",
        "d": "All of above"
    }
},
{
    "1": "Pili",
    "2": "Survival",
    "3": "None of the above",
    "4": "Robert Koch",
    "5": "Flagella",
    "6": "Drug resistant having disease and insect resistant gene",
    "7": "Support",
    "8": "Peritrichous",
    "9": "Respiration",
    "10": "Flygella",
    "11": "Slime",
    "12": "Cell membrane",
    "13": "Lacking sterol i.e. cholesterol",
    "14": "Mycoplasma",
    "15": "Peptidoglycan",
    "16": "More lipids and less protein",
    "17": "Cocci"
}

]

Вот мой код:

   class ResultPage extends StatelessWidget {
  int marks;
  int i = 1;
  var mydata;
  var length;

  ResultPage(this.marks, this.mydata, this.length);

@override 
 Widget build(BuildContext context) {
    return FinalResultCard(marks: marks, mydata: mydata, length: length,); 
  }
}


class FinalResultCard extends StatefulWidget {
  int marks;
  int i = 1;
  var mydata;
  var length;

  FinalResultCard({Key key, this.length, this.mydata, this.marks} ) : super(key: key);

  @override
  _FinalResultCardState createState() => _FinalResultCardState(length, mydata, marks);
}
class _FinalResultCardState extends State<FinalResultCard> {
   int marks;
  int i = 1;
  var mydata;
  var length;

  _FinalResultCardState(this.length, this.mydata, this.marks);


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Appbar ")
      ),
      body: Container(
        color: Colors.red,
        child: ListView.builder
  (
    itemCount: 1,
    itemBuilder: (BuildContext ctxt, int index) {
        for(int i = 1; i<length; i++){
        var data = mydata[0][i.toString()];
        }

     return Text(data);

    }),
      ),
    );
  }
}

Я не понимаю, что делаю здесь неправильно. Заранее ищу помощи и спасибо.

0 ответов

Другие вопросы по тегам