嵌套到Lisview Builder的Flutter JSON

时间:2019-02-14 11:18:59

标签: mysql json listview flutter

抱歉,我已经在youtube和google上进行了搜索,但是我仍然没有任何线索来解析嵌套在listview构建器中的JSON。

我的json结果

[
    {
        "ID_Type": "1",
        "Type": "Food",
        "Item": [
            {
                "SLU_Number": "3",
                "SLU_Name": "Food"
            }
        ]
    },
    {
        "ID_Type": "2",
        "Type": "Beverages",
        "Item": [
            {
                "SLU_Number": "1",
                "SLU_Name": "Non Alcohol"
            },
            {
                "SLU_Number": "2",
                "SLU_Name": "Alchohol"
            }
        ]
    }
]

我希望ID_Type,Type,Item(SLU_Number和SLU名称)将所有值都扔给Listview.builder

谢谢。

我在Flutter上的代码,我在youtube上遵循一些代码,然后将值扔到List,但是我不怎么将值扔到Listview.builder

class Products {
  final ID_Type;
  final Name_Type;
  final Items;

  Products({
    this.ID_Type,
    this.Name_Type,
    this.Items,
  });

  factory Products.fromJson(Map<String, dynamic> json) {
    return Products(
      ID_Type: json["ID_Type"],
      Name_Type: json["Type"],
      Items: Item.fromJson(json["Item"]),
    );
  }
}

class Item {
  final SLU_Number;
  final SLU_Name;

  Item({
    this.SLU_Number,
    this.SLU_Name,
  });

  factory Item.fromJson(Map<String, dynamic> json) {
    return Item(
      SLU_Number: json["SLU_Number"],
      SLU_Name: json["SLU_Name"],
    );
  }
}

以下是我将所有“ json”结果扔到列表中的未来

    //------------------------------Start Syntax for List SLU
    final listslu = new List<ListSLU>();
    final GlobalKey<RefreshIndicatorState> _refreshlistslu =
        GlobalKey<RefreshIndicatorState>();


    Future<void> ListSLUs() async {
      listslu.clear();
      setState(() {
        loading = true;
      });

      try {
        final response = await http.get(BaseUrl.ListSLU);

        if (response.contentLength == 2) {
        } else {
          final data = jsonDecode(response.body);
          data.forEach((api) {
            final b = new ListSLU(
              api['ID_Type'].toString(),
              api['SLU_Number'].toString(),
              api['SLU_Name'].toString(),
            );

         listslu.add(b);
         print(api['SLU_Name'].toString());
      });
    }
  } catch (e) {
      print("Error List SLU :");
      print(e);
  }
}

  //------------------------------End Syntax for List Type

解析所有json结果,然后将所有json中的值扔给正文

Expanded(
          flex: 2,
          child: ListView.builder(
            itemCount: listslu.length,
            itemBuilder: (context,i){
              final z = listslu[i];
              return GestureDetector(
                onTap: (){

                },
                child: Container(
                  child: Column(
                    children: <Widget>[
                      Row(
                        children: <Widget>[
                      Row(
                      children: <Widget>[
                      Text(z.SLU_Number +' - '+ z.SLU_Name),
                    ],
                  ),
                        ],
                      )
                    ],
                  ),
                ),
              );
            }
          )
      ),

0 个答案:

没有答案