简体   繁体   English

颤振水平ListView.builder()不起作用

[英]Flutter Horizontal ListView.builder() not working

I'm making an API call, which gives back a list .我正在做一个 API 调用,它返回一个list I want to display, let's say, SomeWidget() horizontally using ListView.builder() .我想使用ListView.builder() SomeWidget() ) 。 But I'm not able to achieve that target still.但我仍然无法实现这个目标。

I've tried wrapping the ListView.builder() inside a Container() and gave it some height and width.我尝试将ListView.builder()包装在Container()中并给它一些高度和宽度。 But hard luck.但运气不好。 I've also tried wrapping it using Expanded() .我也尝试过使用Expanded()包装它。 But hard luck.但运气不好。 I have tried wrapping it with, first a Container() then wrapping the Container() with Expanded() .我试过用Container()包装它,然后用Expanded()包装Container() ) 。 Still hard luck.还是运气不好。

The error that I keep getting is the following:我不断收到的错误如下:

'package:flutter/src/rendering/sliver_multi_box_adaptor.dart': Failed assertion: line 544 pos 12: 'child.hasSize': is not true.
The relevant error-causing widget was
ListView

Here's my ListView.builder() code:这是我的ListView.builder()代码:

someList.isNotEmpty ? 
  Padding(
    padding: const EdgeInsets.all(8.0),
    child: Container(
   child: Column(
   children: [
   Align(
    alignment: Alignment.topLeft,
    child: Padding(
    padding: const EdgeInsets.symmetric(
    horizontal: 12, vertical: 6),
    child: Text('HEADING',
    style: TextStyle(fontSize: 12),
   ),
    ),
       ),
        Container(
           height: 15.h,
           width: 80.w,
           child: ListView.builder(
           scrollDirection: Axis.horizontal,
           itemCount: someList.length,
           itemBuilder: (_, index) {
           return SomeWidget();
          }),
         ),
        ],
      ),
    ),
   ) : Text(''),

Is there any other information that I should add here?我应该在这里添加任何其他信息吗?

Update The ListView.builder() is working perfectly for vertical scrolling.更新ListView.builder() 非常适合垂直滚动。 The code is the following:代码如下:

Container(
   height: 15.h,
   width: double.infinity,
   child: ListView.builder(
   // scrollDirection: Axis.horizontal,
   itemCount: someList.length,
   itemBuilder: (_, index) {
   return SomeWidget();
   },
  ),
),

On adding scrollDirection: Axis.horizontal , it however doesn't work.在添加scrollDirection: Axis.horizontal时,它不起作用。

Add Your ListView.builder() inside Expanded or Flex Widget hope its help to youExpanded 或 Flex Widget 中添加您的ListView.builder()希望它对您有所帮助

Column(
     mainAxisSize: MainAxisSize.min,
     children: <Widget>[
     Expanded(
      child:ListView.builder(
       shrinkWrap: true,
       scrollDirection: Axis.horizontal,
       itemCount: someList.length,
       itemBuilder: (_, index) {
           return SomeWidget();
     }),
    ),
   ],
  ),
Padding(
    padding: const EdgeInsets.all(8.0),
    child: Container(
    //Try add height and width
   child: Column(
   mainAxisAlignment: MainAxisAlignment.start,
   crossAxisAlignment: CrossAxisAlignment.start,
   textDirection : TextDirection.ltr or rtl
   children: [
   Align(
    Padding(
    padding: const EdgeInsets.symmetric(
    horizontal: 12, vertical: 6),
    child: Text('HEADING',
    style: TextStyle(fontSize: 12),
   ),
       ),

i think or maybe the listview not the problem maybe inside on the listview item is the problem which as the error you've shown is that need has szie so in between in those item need to be wrap container or sized box我认为或者列表视图不是问题,可能是列表视图项目内部的问题,因为您显示的错误是需要有 szie,因此在这些项目之间需要包装容器或大小的盒子

If Expanded or Flexible widgets didn't work, try giving a fixed width to your ListView items.如果ExpandedFlexible小部件不起作用,请尝试为您的ListView项目提供固定宽度

Column(
     children: [
      child:ListView.builder(
       scrollDirection: Axis.horizontal,
       itemCount: someList.length,
       itemBuilder: (_, index) {
           return Container(width: 150, child: someWidget());
     }),
    ),
   ],
  ),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM