如何从带有循环中json数据的for循环返回小部件?

时间:2020-04-08 13:25:29

标签: flutter dart flutter-widget

我在迭代json索引的for循环时遇到问题,并且我期望从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 个答案:

没有答案