【问题标题】:How to display selected month data from flutter dropdown selection using json如何使用json从颤振下拉选择中显示选定的月份数据
【发布时间】:2021-04-16 09:20:48
【问题描述】:

我尝试在 flutter 的下拉选择中显示选定的月份数据,假设我选择一月份然后它以 json 格式显示一月份的月份记录

String monthWiseGraph;
Future<String> getMonthlyGraphData() async {
String url = 'http://example.com/getAgentMonthGraph.php?month='+ monthWiseGraph ;
var response = await http.get(url);
return response.body;
}
Container(
        height: 130,
        width: 200,
        padding: EdgeInsets.only(left: 30, top: 50),
        child: Card(
          // color: Colors.blueGrey,
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.only(
                  topRight: Radius.circular(20),
                  bottomLeft: Radius.circular(20)),
              side: BorderSide(color: Colors.black)),
          child: Column(
            children: <Widget>[
              Container(
                padding: EdgeInsets.only(top: 10, right: 20, left: 20),
                child: DropdownButtonHideUnderline(
                  child: DropdownButton(
                    hint: Text("Monthly",
                        style:
                            TextStyle(color: Colors.black, fontSize: 25)),
                    value: monthWiseGraph,
                    icon: Icon(Icons.arrow_drop_down),
                    iconSize: 30,
                    elevation: 16,
                    style: TextStyle(color: Colors.black),
                    onChanged: (String monthNewValue) {
                      setState(
                        () {
                          monthWiseGraph = monthNewValue;
                          print(monthWiseGraph);
                          _showMonthlyAlert();
                        },
                      );
                    },
                    items: <String>[
                      'January',
                      'February',
                          .
                          .
                      'December'
                    ].map<DropdownMenuItem<String>>((String value) {
                      return DropdownMenuItem<String>(
                        value: value,
                        child: Text(
                          value,
                          style: TextStyle(
                            fontSize: 20,
                          ),
                        ),
                      );
                    }).toList(),
                  ),
                ),                  
              ),
            ],
          ),
        ),
      ),

这是我一月份的数据

[
  {
      userId: "2",
      name: "Manish",
      total: "32"
     },
    {
       userId: "3",
      name: "Altaf",
      total: "31"
       }
  ]

这是我二月份的数据

[
  {
    userId: "4",
    name: "Prathamesh",
    total: "68"
  },
  {
   userId: "7",
   name: "Aniket",
   total: "62"
  },
  ]

上图我显示选定的月份数据当我从下拉列表中选择 1 月时,它会显示来自 json 的 1 月数据,如 manish 和 altaf,当我选择 2 月时,它会显示 Prathamesh 和 Aniket 数据,下面是正常图 我是新来的颤振

This is my drop down

【问题讨论】:

    标签: json flutter


    【解决方案1】:

    您必须将“jsonDecode(response.body)”放在变量中,或者只需更改“return”关键字。然后您可以访问 JSON api,因为“JsonDecode”会将 JSON 数据更改为 Flutter 语言。确保“导入”所需的文件。

    【讨论】:

    • 先生,我的网址如example.com/getAgentMonthGraph.php?month=March(这是静态网址),这给了我回复,但我在下拉菜单中选择月份,
    • 我猜“url”是问题,因为当我点击链接时,它给了我“404 code”
    • 更新后的 url 为 us.rdigs.com/API/getAgentMonthGraph.php?month=March 请替换为 2 月/4 月等
    • 尝试将“DropdownMenu”“onChange”值更改为 () { setState() {getMonthlyGraphData}}
    • @Aiman_Irfan 先生,这是行不通的,我的问题是当我选择 1 月时,它会显示 1 月数据getAgentMonthGraph.php?month='+monthWiseGraph;
    猜你喜欢
    • 2022-11-28
    • 2019-07-26
    • 2013-06-20
    • 1970-01-01
    • 2020-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多