Flutter ListView 在 Column 中正常工作,但在 Row 中不正常工作。

huangapple go评论71阅读模式
英文:

Flutter ListView working in Column but not in Row

问题

错误信息:

在运行以下代码时出现错误。

Scaffold(
  body: SafeArea(
    child: SingleChildScrollView(
      scrollDirection: Axis.vertical,
      child: Row(
         mainAxisSize: MainAxisSize.max,
         children: [
           ListView.builder(
             shrinkWrap: true,
             itemCount: rideInfo[0]['matched'][0]['companionInfo'].length,
             itemBuilder: (context, index) {
               return Container(
                 color: Colors.red,
                 height: 40,
                 width: 40,
                 child: Text("Test"),
               );
              },
          ),
        ],
      ),
    ),
  ),
)

错误信息:

在执行布局期间引发了以下断言:

'package:flutter/src/rendering/viewport.dart':断言失败:line 1890 pos 16:'constraints.hasBoundedWidth' 不为 true。

package:flutter/…/rendering/viewport.dart:1890

2

这个断言可能表示了框架本身的错误,或者我们应该在此错误消息中提供更多信息,以帮助您确定和修复根本原因。

在任何情况下,请通过在GitHub上提交错误来报告此断言:

https://github.com/flutter/flutter/issues/new?template=2_bug.md

引发错误的相关部件是ListView。

解决方法:

如果将 Row 更改为 Column,似乎可以正常工作。您可以如何修复此错误?

英文:

I am trying to populate some data in a row. But whenever I run the following code it gives error.

Code

Scaffold(
  body: SafeArea(
    child: SingleChildScrollView(
      scrollDirection: Axis.vertical,
      child: Row(
         mainAxisSize: MainAxisSize.max,
         children: [
           ListView.builder(
             shrinkWrap: true,
             itemCount: rideInfo[0]['matched'][0]['companionInfo'].length,
             itemBuilder: (context, index) {
               return Container(
                 color: Colors.red,
                 height: 40,
                 width: 40,
                 child: Text("Test"),
               );
              },
          ),
        ],
      ),
    ),
  ),
)

Error

The following assertion was thrown during performLayout():
'package:flutter/src/rendering/viewport.dart': Failed assertion: line 1890 pos 16: 'constraints.hasBoundedWidth': is not true.
package:flutter/…/rendering/viewport.dart:1890
2

Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
  https://github.com/flutter/flutter/issues/new?template=2_bug.md

The relevant error-causing widget was
ListView

However if I change Row to Column, it seems to be working fine. How can I fix the error?

答案1

得分: 3

你不能在相同的组件树中使用两个无限滚动小部件。您在相同的组件树中同时使用了SingleChildScrollViewListView。您需要更改如下:

Scaffold(
  body: SafeArea(
    child: SingleChildScrollView(
      scrollDirection: Axis.vertical,
      child: Row(
        mainAxisSize: MainAxisSize.max,
        children: [
          SizedBox(
            height: 50,
            child: ListView.builder(
              scrollDirection: Axis.horizontal,
              shrinkWrap: true,
              itemCount: rideInfo[0]['matched'][0]['companionInfo'].length,
              itemBuilder: (context, index) {
                return Container(
                  color: Colors.red,
                  height: 40,
                  width: 40,
                  child: Text("Test"),
                );
              },
            ),
          )
        ],
      ),
    ),
  ),
)
英文:

You can not two infinite scroll widget in the same widget tree. You are using SingleChildScrollView and Listview both in the same widget tree. You have change this like:

Scaffold(
  body: SafeArea(
    child: SingleChildScrollView(
      scrollDirection: Axis.vertical,
      child: Row(
         mainAxisSize: MainAxisSize.max,
         children: [
              SizedBox(
                  height: 50,   
                  child:ListView.builder(
                       scrollDirection:Axis.horizontal,
                       shrinkWrap: true,
                       itemCount: rideInfo[0]['matched'][0]['companionInfo'].length,
             itemBuilder: (context, index) {
               return Container(
                 color: Colors.red,
                 height: 40,
                 width: 40,
                 child: Text("Test"),
               );
              },
          ),
              )
        ],
      ),
    ),
  ),
)

huangapple
  • 本文由 发表于 2023年2月10日 14:52:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定