仅当我单击“搜索"表单或热重新加载时,才会填充ListView [英] ListView populated only if I click the Search form or hot reload

查看:64
本文介绍了仅当我单击“搜索"表单或热重新加载时,才会填充ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class _SearchState extends State<Search> {
  List  _searchResult = [];


  TextEditingController controller = new TextEditingController();


  var userDetails  = {};
  List returnTicketDetails;

  @override
  Widget build(BuildContext context) {

    body: new Column(
        children: <Widget>[
          new Container(

            color: Theme.of(context).primaryColor,

            child: new Padding(

              padding: const EdgeInsets.all(8.0),
              child: new Card(
                child: new ListTile(
                  leading: new Icon(Icons.search),
                  title: new TextField(
                    controller: controller,
                    decoration: new InputDecoration(
                        hintText: 'Search', border: InputBorder.none),
                    onChanged: onSearchTextChanged,
                  ),
                  trailing: new IconButton(icon: new Icon(Icons.cancel), onPressed: () {
                    controller.clear();
                    onSearchTextChanged('');
                  },),
                ),
              ),
            ),
          ),
          new Expanded(

            child: _searchResult.length != 0 || controller.text.isNotEmpty ?
            new ListView.builder(

               itemCount: _searchResult.length,
              itemBuilder: (context, i) {

                return new Card(

                  child: new Column
                    (mainAxisSize: MainAxisSize.min, children:
                    <Widget>[
                    new Row(children: <Widget>[
                    new Container(
                    width: 80.0,
                    height: 80.0,
                    decoration: new BoxDecoration(

                    shape: BoxShape.circle,
                    image: new DecorationImage(
                    fit: BoxFit.fill,
                    image: new NetworkImage(

                    "https:.....")
                )
                )),
                    new Text(" " + userDetails[returnTicketDetails[i]["user_id"]]["first_name"] +  " " + (userDetails[returnTicketDetails[i]["user_id"]]["last_name"]),


                ])
                    ])
                );
                },
            )
                : new ListView.builder(
               itemCount: _searchResult.length,
              itemBuilder: (context, i) {
                return new Card(
                  child: new ListTile(
                                     ),
                  margin: const EdgeInsets.all(0.0),
                );
              },
            ),
          ),
        ],
      ),

      onSearchTextChanged(String text) async {

            _searchResult.clear();
        if (text.isEmpty) {
          for (int i = 0; i< returnTicketDetails.length;  i++){
            _searchResult.add(userDetails[returnTicketDetails[i]["user_id"]]);
          }
          setState(() {});
          return;
        }


        for (int i = 0; i< returnTicketDetails.length;  i++){

          if (userDetails[returnTicketDetails[i]["user_id"]]["first_name"].toString().toLowerCase().contains(text.toLowerCase()) || userDetails[returnTicketDetails[i]["user_id"]]["last_name"].toString().toLowerCase().contains(text.toLowerCase())
              || returnTicketDetails[i]["code"].toString().toLowerCase().contains(text.toLowerCase())) {
            _searchResult.add(userDetails[returnTicketDetails[i]["user_id"]]);

          }
        }
        setState(() {});
      }

我想知道为什么当我进入应用程序的该屏幕时,即使我希望列表中的元素很少,ListView还是空的.但是,只有当我单击搜索栏"字段或热重载"按钮时,数据才会被加载,并且ListView会填充正确的数据?理想情况下,默认情况下,我想让ListView在进入此屏幕后立即显示List中的所有数据,然后根据用户输入开始搜索List元素中的过滤器.预先谢谢你

I was wondering why when I go into this screen of my app the ListView is empty even though I am expecting to have few elements in the List. However only once I hit the Search bar field or hot reload button, the data gets loaded and my ListView gets populated with the right data??. Ideally as default I would like to have the ListView displaying all the data in the List as soon as I come in this screen, and then start to search filter through the element of my List according to user input. Thank you in advance

推荐答案

I guess the problem is with the line
**child: _searchResult.length != 0 || controller.text.isNotEmpty ?**

since you don't have anything in the _searchResult when the app loads first time your

**new ListView.builder(
               itemCount: _searchResult.length,
              itemBuilder: (context, i) {
                return new Card(
                  child: new ListTile(
                                     ),
                  margin: const EdgeInsets.all(0.0),**

is being rendered with no content to display and when you start typing in the text box onSearchTextChanged is called and the other ListView.builder is rendered with some data (as in your case 2,3 items in list).

Hope it helps!!!

这篇关于仅当我单击“搜索"表单或热重新加载时,才会填充ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆