【问题标题】:Check if ListView.builder returns only empty Containers检查 ListView.builder 是否仅返回空容器
【发布时间】:2021-04-23 11:34:24
【问题描述】:

我有一个由 ListView.builder 提供的列表,我使用一个函数来检查是否正在搜索列表中的对象,但我想在没有找到对象时显示一个屏幕。所以用户知道什么都没有找到,但我正在努力解决它,代码看起来像这样:

Container OnlyShow = Container(
    child: ListView.builder(
      itemCount: myList.length,
      itemBuilder: (context, index) {
        if (searchfunction(myList[index]) == true) {
          return myListObject(myList[index]);
        } else {
          return Container(height: 0, width: 0,);
        }
      },
    ),
  );

搜索功能本身不是问题我只想知道如何找出 ListView.builder 是否只返回空容器,因为这意味着没有对象等于搜索,因此什么也找不到。

我尝试过类似的方法:

Container OnlyShow = Container(
    child: ListView.builder(
      itemCount: myList.length,
      itemBuilder: (context, index) {
        if (searchfunction(myList[index]) == true) {
          if (howManyObjectsAreFalse > 0) {
            howManyObjectsAreFalse--;
            dismissNoResultsFoundScreen();
          }
          return myListObject(myList[index]);
        } else {
          if (howManyObjectsAreFalse < myList.length) {
            howManyObjectsAreFalse++;
          }
          if (howManyObjectsAreFalse == myList.length) {
            showNoResultsFoundScreen();
          }
          return Container(height: 0, width: 0,);
        }
      },
    ),
  );
  
  int howManyObjectsAreFalse = 0;

当返回一个空的 Container 时,它应该像这样工作,当返回一个 Result 时,计数会减少,所以当计数达到列表的长度时,就会显示 NoResultScreen。

它确实有效,但有一些奇怪的例外情况,有时计数似乎在值之间波动,即使 ListView.builder 只返回空容器。

注意:列表不是空的,我只是为列表中不等于搜索值的每个对象返回一个空容器。所以我不会减少列表什么的

我希望有人可以帮助我以更好的方式实现这一目标。

【问题讨论】:

  • 列表不为空
  • 没有找到任何东西:当只返回空容器时,myList 保持不变,我不更改myList 我只查看searchfunction(myList) == true 和如果我返回myList[index] 和如果不是,我返回一个空容器
  • child: myList.where(searchfunction).isEmpty ? Text('nothing was found') : ListView.builder(...)
  • 但老实说返回这样的Container(height: 0, width: 0,); 是错误的:您应该删除不存在的数据,而不是显示空的小部件
  • 顺便说一句,你不能使用 tmp 数组,如:final tempArray = myList.where(searchfunction).toList(); 吗?并使用它:tempArray.isEmpty? Text('nothing found') : ListView.builder(itemCount: tempArray.length, ...

标签: flutter dart


【解决方案1】:

我不确定这是你想要的

int howManyObjectsAreFalse = myList.length;
Container OnlyShow = Container(
    child: ListView.builder(
      itemCount: myList.length,
      itemBuilder: (context, index) {
        if (searchfunction(myList[index]) == true) {
            howManyObjectsAreFalse--;
          if (howManyObjectsAreFalse < myList.length) {
            dismissNoResultsFoundScreen();
          }
          return myListObject(myList[index]);
        } else {
          if (howManyObjectsAreFalse == myList.length) {
            showNoResultsFoundScreen();
          }
          return Container(height: 0, width: 0,);
        }
      },
    ),
  );

【讨论】:

    【解决方案2】:

    @pskink 给出的答案成功了:

    child: myList.where(searchfunction).isEmpty ? Text('nothing was found') : ListView.builder(...)
    

    【讨论】:

    • 顺便说一句,如果您真的想这样做并使用“隐形”容器,那么您可以使用更快(并且使用更少内存)的方法:child: myList.any(searchfunction) ? ListView.builder(...) : Text('nothing was found')
    【解决方案3】:

    ListView.builder 是一个惰性列表构建器,因此它只会构建当前在屏幕上呈现的子级。 不过你可以这样做。

      int searchResultsCnt = 0;
      final shownList = myList.map<Widget>((e) {
        if (searchFunction(e)) {
          searchResultsCnt++;
          return myListObject(e);
        }
        return Container(height: 0, width: 0);
      }).toList<Widget>();
      
      if (searchResultsCnt == 0)
        showNoResultsFoundScreen();
      else
        dismissNoResultsFoundScreen();
      
      return ListView.builder(
        itemCount: shownList.length,
        itemBuilder: (context, index) => shownList[index],
      );
    

    【讨论】:

      【解决方案4】:

      ListView.builder itemCount 没有数据在正文中显示文本时的示例,

      if (customerRegList.length != 0) {
            return ListView.builder(
                shrinkWrap: true,
                scrollDirection: Axis.vertical,
                itemCount: customerRegList == null ? 0 : customerRegList.length,
                itemBuilder: (BuildContext context, int index) {
                  return Card(
      child:Text('Hello World'),
       );
                });
          } else {
            return Center(
              child: Padding(
                padding: const EdgeInsets.only(top: 80.0),
                child: Text(
                  'No Data Available!!',
                  style: TextStyle(color: Colors.red, fontSize: 20.0, fontWeight: FontWeight.bold),
                ),
              ),
            );
          }
      

      【讨论】:

        猜你喜欢
        • 2017-10-16
        • 2011-08-22
        • 2021-02-03
        • 2010-09-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多