Dart 仅当单击搜索表单或热重新加载时,才会填充ListView class\u SearchState扩展状态{ 列表_searchResult=[]; TextEditingController=新的TextEditingController(); var userDetails={}; 列出返回的详细信息; @凌驾 小部件构建(构建上下文){ 正文:新栏目( 儿童:[ 新容器( 颜色:主题。背景。原色, 孩子:新的填充物( 填充:常数边集全部(8.0), 孩子:新卡( 孩子:新的ListTile( 前导:新图标(Icons.search), 标题:新文本字段( 控制器:控制器, 装饰:新的输入装饰( hintText:“搜索”,边框:InputBorder.none), onChanged:onSearchTextChanged, ), 尾随:新建图标按钮(图标:新建图标(图标.取消),打开按钮:(){ controller.clear(); onSearchTextChanged(“”); },), ), ), ), ), 新扩展( 子项:_searchResult.length!=0 | | controller.text.isNotEmpty? 新建ListView.builder( itemCount:\u searchResult.length, itemBuilder:(上下文,i){ 归还新卡( 子:新列 (mainAxisSize:mainAxisSize.min,子项: [ 新行(儿童:[ 新容器( 宽度:80.0, 身高:80.0, 装饰:新盒子装饰( 形状:BoxShape.circle, 图片:新装饰图片( fit:BoxFit.fill, 图像:新网络图像( “https:…”) ) )), 新文本(“+userDetails[returnTicketDetails[i][“user\u id”]][“first\u name”]+”+(userDetails[returnTicketDetails[i][“user\u id”][“last\u name”]), ]) ]) ); }, ) :new ListView.builder( itemCount:\u searchResult.length, itemBuilder:(上下文,i){ 归还新卡( 孩子:新的ListTile( ), 边距:所有常数边集(0.0), ); }, ), ), ], ), onSearchTextChanged(字符串文本)异步{ _searchResult.clear(); if(text.isEmpty){ for(int i=0;i

Dart 仅当单击搜索表单或热重新加载时,才会填充ListView class\u SearchState扩展状态{ 列表_searchResult=[]; TextEditingController=新的TextEditingController(); var userDetails={}; 列出返回的详细信息; @凌驾 小部件构建(构建上下文){ 正文:新栏目( 儿童:[ 新容器( 颜色:主题。背景。原色, 孩子:新的填充物( 填充:常数边集全部(8.0), 孩子:新卡( 孩子:新的ListTile( 前导:新图标(Icons.search), 标题:新文本字段( 控制器:控制器, 装饰:新的输入装饰( hintText:“搜索”,边框:InputBorder.none), onChanged:onSearchTextChanged, ), 尾随:新建图标按钮(图标:新建图标(图标.取消),打开按钮:(){ controller.clear(); onSearchTextChanged(“”); },), ), ), ), ), 新扩展( 子项:_searchResult.length!=0 | | controller.text.isNotEmpty? 新建ListView.builder( itemCount:\u searchResult.length, itemBuilder:(上下文,i){ 归还新卡( 子:新列 (mainAxisSize:mainAxisSize.min,子项: [ 新行(儿童:[ 新容器( 宽度:80.0, 身高:80.0, 装饰:新盒子装饰( 形状:BoxShape.circle, 图片:新装饰图片( fit:BoxFit.fill, 图像:新网络图像( “https:…”) ) )), 新文本(“+userDetails[returnTicketDetails[i][“user\u id”]][“first\u name”]+”+(userDetails[returnTicketDetails[i][“user\u id”][“last\u name”]), ]) ]) ); }, ) :new ListView.builder( itemCount:\u searchResult.length, itemBuilder:(上下文,i){ 归还新卡( 孩子:新的ListTile( ), 边距:所有常数边集(0.0), ); }, ), ), ], ), onSearchTextChanged(字符串文本)异步{ _searchResult.clear(); if(text.isEmpty){ for(int i=0;i,dart,flutter,Dart,Flutter,我想知道为什么当我进入我的应用程序的这个屏幕时,ListView是空的,尽管我希望列表中只有很少的元素。但是只有当我点击搜索栏字段或热重新加载按钮时,数据才会被加载,并且我的ListView会被正确的数据填充??。 理想情况下,默认情况下,我希望列表视图在我进入此屏幕后立即显示列表中的所有数据,然后根据用户输入开始搜索筛选我列表中的元素。 提前感谢您谢谢您的建议,我实际上认为问题可能已经存在,但是我不知道如何进一步编辑它,您对实现有什么建议吗?谢谢您覆盖“void initState()”,它将

我想知道为什么当我进入我的应用程序的这个屏幕时,ListView是空的,尽管我希望列表中只有很少的元素。但是只有当我点击搜索栏字段或热重新加载按钮时,数据才会被加载,并且我的ListView会被正确的数据填充??。 理想情况下,默认情况下,我希望列表视图在我进入此屏幕后立即显示列表中的所有数据,然后根据用户输入开始搜索筛选我列表中的元素。
提前感谢您

谢谢您的建议,我实际上认为问题可能已经存在,但是我不知道如何进一步编辑它,您对实现有什么建议吗?谢谢您覆盖“void initState()”,它将在小部件第一次呈现时调用,并调用您的onSearchTextChanged()里面。您好!谢谢您的回复!我已经按照您所说的做了尝试,但是每当我进入ListView屏幕时,我都会得到:E/flatter(3973):NoSuchMethodError:getter'length'是在null上调用的。E/flatter(3973):Receiver:null E/flatter(3973):尝试调用:length
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(() {});
      }
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!!!