如果值匹配,则从JSON读取数据波动

时间:2018-09-17 07:00:58

标签: dart flutter

我有一个客户的json:

customer.json

[
  {
    "name": "Customer 1",
    "id": "1"
  },
  {
    "name": "Customer 2",
    "id": "2"
  },
  {
    "name": "Customer 3",
    "id": "3"
  }
]

这是使用以下json数据的dart文件:

customerslist.dart

未来的方法

Future<String> loadCustomers() async{
  var res = await http.get(
      Uri.encodeFull(url),
      headers: {"Accept": "application/json"});
  return res.body;
}

小部件

widgets.add(new FutureBuilder(
              future: loadCustomers(),
              builder: (context, snapshot){
                if(snapshot.hasData){

                  //get snapshot data from JSON tree
                  var jsondecode = json.decode(snapshot.data);

                  return new ListView.builder(
                      shrinkWrap: true,
                      itemCount: jsondecode.length,
                      itemBuilder: (context, index){
                        String name = jsondecode[index]["name"];
                        String id = jsondecode[index]["id"];

                        if(name == "Customer 2"){
                          return new Column(
                            children: <Widget>[

                              new ListTile(
                                title: new Text("Name"),
                                trailing: new Text(name),
                              ),
                              new ListTile(
                                title: new Text("ID"),
                                trailing: new Text(id),
                              )
                            ],
                          );
                        }
                      });
                }else{
                  return new Center(
                    child: new CircularProgressIndicator(),
                  );
                }
              }));

我想做的是,如果名称与if(name == "Customer 2")相同,则带出值。

但是我遇到的问题是:

当我将if语句更改为if(name == "Customer 1")时,输出与您期望的一样:

First output

但是,如果我将其更改为if(name == "Customer 2"),则输出将变成空白:

Second output

有人可以解释为什么会这样吗?还是我可以采取另一种方法来解决此问题?

2 个答案:

答案 0 :(得分:1)

我认为这是因为Column小部件正在占据整个高度。您可以尝试使用列的最小高度并添加其他条件

                            if(name == "Customer 2"){
                      return new Column(
                        children: <Widget>[

                          new ListTile(
                            title: new Text("Name"),
                            trailing: new Text(name),
                          ),
                          new ListTile(
                            title: new Text("ID"),
                            trailing: new Text(id),
                          )
                        ],
                      );
                    } else {
                    return new Container();
                    }

当您使用“客户1”时,它的工作原理很好,因为您的第一个元素是“客户1”,因为其他项目没有返回的小部件,因此您的控制台上可能出现错误。

对于“客户2”,不会返回第一项,因此请检查您的控制台日志。

itemBuilder希望您在所有情况下都返回一个小部件。

答案 1 :(得分:0)

您只需将var字符串转换为list

var jsondecode = json.decode(snapshot.data);

List list = jsondecode;
 String name = list[index]["name"];
                    String id = list[index]["id"];

然后传递列表以获取索引,这可能会正常工作,因为var类型可能仅获取第一个json字符串

enter code here