草庐IT

Flutter ListView 在列内崩溃

coder 2023-07-21 原文

我需要一些帮助来弄清楚如何呈现 ListView。

我一直在学习 Flutter 教程,但我不得不停止,因为我无法解决这个问题。

据我所知,ListView 试图占用无限量的空间,这显然会使应用程序崩溃。

我也开始明白你不能将 ListView 作为 Column/Row 的直接子级(我在下面解释了我试图做的事情) https://flutter.io/docs/development/ui/layout/box-constraints#flex

代码如下:

@override
Widget build(BuildContext context) {
  return Column(
    children: <Widget>[
      Container(
        margin: EdgeInsets.all(10),
        child: ProductControl(_addProduct),
      ),
      ListView.builder(
        itemCount: _products.length,
        itemBuilder: (BuildContext context, int index) => Card(
              child: Column(
                children: <Widget>[
                  Image.asset('assets/food.jpg'),
                  Text(_products[index])
                ],
              ),
            ),
      )
    ],
  );
}

这就是堆栈跟踪的开头所说的内容:

flutter: ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
flutter: The following assertion was thrown during performResize():
flutter: Vertical viewport was given unbounded height.
flutter: Viewports expand in the scrolling direction to fill their 
container.In this case, a vertical
flutter: viewport was given an unlimited amount of vertical space in 
which to expand. This situation
flutter: typically happens when a scrollable widget is nested inside 
another scrollable widget.
flutter: If this widget is always nested in a scrollable widget there 
is no need to use a viewport because
flutter: there will always be enough vertical space for the children. 
In this case, consider using a Column
flutter: instead. Otherwise, consider using the "shrinkWrap" property 
(or a ShrinkWrappingViewport) to size
flutter: the height of the viewport to the sum of the heights of its children.

这是从堆栈跟踪的底部获取的:

flutter: The following RenderObject was being processed when the 
exception was fired:
flutter:   RenderViewport#cab62 NEEDS-LAYOUT NEEDS-PAINT
flutter:   creator: Viewport ← _ScrollableScope ← IgnorePointer- 
[GlobalKey#b71f9] ← Semantics ← Listener ←
flutter:   _GestureSemantics ← RawGestureDetector- 
[LabeledGlobalKey<RawGestureDetectorState>#d0420] ←
flutter:   _ScrollSemantics-[GlobalKey#02b55] ← Scrollable ← 
PrimaryScrollController ← ListView ← Column ← ⋯
flutter:   parentData: <none> (can use size)
flutter:   constraints: BoxConstraints(0.0<=w<=375.0, 0.0<=h<=Infinity)
flutter:   size: MISSING
flutter:   axisDirection: down
flutter:   crossAxisDirection: right
flutter:   offset: ScrollPositionWithSingleContext#892dc(offset: 0.0, 
range: null..null, viewport: null,
flutter:   ScrollableState, AlwaysScrollableScrollPhysics -> 
BouncingScrollPhysics, IdleScrollActivity#9455f,
flutter:   ScrollDirection.idle)
flutter:   anchor: 0.0
flutter: This RenderObject had the following descendants (showing up to 
depth 5):
flutter:   RenderSliverPadding#c8ab3 NEEDS-LAYOUT NEEDS-PAINT
flutter:     RenderSliverList#66f1b NEEDS-LAYOUT NEEDS-PAINT

我曾尝试将 ListView.builder 包装在扩展小部件中,但这对我不起作用。 (这是教程中正在做的事情)

我试图将 Column 包装在 IntrinsicHeight Widget 中,但没有成功。

我设法解决此问题的唯一方法是将 ListView.builder 包装在具有设置高度属性的 Container 小部件中。但是必须使用具有设定高度的 Container 对我来说似乎不合适。

如果需要,我可以尝试发布完整的代码来重新创建它。

最佳答案

尝试使用“Expanded”代替 Container 或其他布局:

Column(
        children: <Widget>[
          Expanded(
            //color: Colors.white,
            child:
            ListView.builder(
                itemCount: list.length,
                itemBuilder: (BuildContext context, int index) {
                  return ListTile(
                    contentPadding: EdgeInsets.all(10.0),
                    title: new Text('title'),
                    subtitle: new Text('sub title'),
                    onTap: () => clicked(list[index]['url']),
                    onLongPress: () => downloadAndStoreFile(list[index]['url'],
                        list[index]['name'], list[index]['title']),
                  );
                }),
          )
        ],
),

关于Flutter ListView 在列内崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53797413/

有关Flutter ListView 在列内崩溃的更多相关文章

  1. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  2. Ruby Readline 在向上箭头上使控制台崩溃 - 2

    当我在Rails控制台中按向上或向左箭头时,出现此错误:irb(main):001:0>/Users/me/.rvm/gems/ruby-2.0.0-p247/gems/rb-readline-0.4.2/lib/rbreadline.rb:4269:in`blockin_rl_dispatch_subseq':invalidbytesequenceinUTF-8(ArgumentError)我使用rvm来管理我的ruby​​安装。我正在使用=>ruby-2.0.0-p247[x86_64]我使用bundle来管理我的gem,并且我有rb-readline(0.4.2)(人们推荐的最少

  3. ruby - 在多个线程中引用类方法会导致自动加载循环依赖崩溃 - 2

    代码:threads=[]Thread.abort_on_exception=truebegin#throwexceptionsinthreadssowecanseethemthreadseputs"EXCEPTION:#{e.inspect}"puts"MESSAGE:#{e.message}"end崩溃:.rvm/gems/ruby-2.1.3@req/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:478:inload_missing_constant':自动加载常量MyClass时检测到循环依赖稍加研究后,

  4. ruby - 执行过期异常使 Ruby 线程崩溃,但处理了 Timeout::Error - 2

    任何人都可以解释为什么当对方法的调用看起来像这样时我可能会看到这个堆栈(由HTTParty::post请求引起):beginresponse=HTTParty::post(url,options)rescuelogger.warn("Couldnotpostto#{url}")rescueTimeout::Errorlogger.warn("Couldnotpostto#{url}:timeout")end堆栈:/usr/local/lib/ruby/1.8/timeout.rb:64:in`timeout'/usr/local/lib/ruby/1.8/net/protocol.rb

  5. ruby - vim 使用 AutoComplPop 插件崩溃 - 2

    我使用vim编辑ruby​​文件,但是当我输入“.”时它崩溃了。我发现它是由AutoComplPop插件引起的。我该怎么办? 最佳答案 我找到了一种使用autocomplpop和filetype=ruby来防止vim崩溃的方法。将以下行放入您的.vimrcletg:acp_behaviorRubyOmniMethodLength=-1这将防止在您键入“.”时触发autocomplpop。(期间)这不是解决办法。(我不是vim插件程序员)祝你好运! 关于ruby-vim使用AutoComp

  6. ruby-on-rails - 自动加载路径和嵌套服务类在 Ruby 中崩溃 - 2

    我在Rails5项目的app/services文件夹下有多个加载/需要类的问题,我开始放弃这个问题。首先要明确的是,services/是我在整个项目中使用的简单PORO类,用于从Controller、模型等中抽象出大部分业务逻辑。树看起来像这样app/services/my_service/base.rbfunny_name.rbmy_service.rbmodels/funny_name.rb失败#1首先,当我尝试使用MyService.const_get('FunnyName')时,它从我的模型目录中获取了FunnyName。当我直接执行MyService::FunnyName时,

  7. ruby - unicorn 与 Ruby 2.4.1 导致奇怪的崩溃 - 2

    我正在从Ruby2.3.1升级到Ruby2.4.1,这样做之后,Unicorn似乎与新版本不兼容。我收到以下错误。我正在使用Unicorn5.1.0并尝试过Unicorn5.3.1无济于事。我是否需要使用不同的库而不是XCode工具进行编译?我在使用foremanstart和Procfile启动服务器后立即收到错误:webpack:bin/webpack-dev-servergulp:gulpredis:./scripts/start_redis_server.shsidekiq:bundleexecsidekiq-Cconfig/sidekiq.ymlannotations_serv

  8. ruby - Heroku 应用程序崩溃并显示 'libruby.so.1.9: cannot open shared object file' - 2

    这个问题不太可能帮助任何future的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visitthehelpcenter.关闭10年前。我在部署这个应用程序时从未遇到过问题,但我刚刚推送,现在我遇到了这个奇怪的错误,它在堆栈中足够深,甚至没有被发送到Airbrake。我觉得这可能是Heroku的问题,可能与https://status.heroku.com/incidents/450有关。?你怎么看?2012-10-14T08:40:31+00:00heroku[web.1]:State

  9. ruby - 带有 ruby​​ 1.9.3 的 Heroku 导致许多不同的崩溃 - 2

    我使用Gemfile中的新gem重新部署我的heroku应用程序,它在启动时崩溃了/app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in`require':libruby.so.1.9:cannotopensharedobjectfile:Nosuchfileordirectory-/app/vendor/bundle/ruby/1.9.1/gems/bcrypt-ruby-3.0.1/lib/bcrypt_ext.so(LoadError)无论我部

  10. ruby-on-rails - ruby 2.0.0p247 的段错误导致 Rails 服务器崩溃 - 2

    在OSXMavericks1.9上运行Rails4.0、Ruby2.0.0p247我遇到了一个我今天从未遇到过但我不理解的错误。当试图在我的项目中启动我的Rails服务器时,服务器崩溃了。我确实尝试重新安装和重新编译一次,成功但在成功运行服务器两次后,它再次开始崩溃。知道是什么原因造成的吗?admins-air:rengaadmin$railss/Users/admin/.rvm/gems/ruby-2.0.0-p247/gems/json-1.8.1/lib/json/ext/parser.bundle:[BUG]Segmentationfaultruby2.0.0p247(2013

随机推荐