简体   繁体   中英

Flutter Horizontal ListView.builder() not working

I'm making an API call, which gives back a list . I want to display, let's say, SomeWidget() horizontally using ListView.builder() . 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. But hard luck. I've also tried wrapping it using Expanded() . But hard luck. I have tried wrapping it with, first a Container() then wrapping the Container() with Expanded() . 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:

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. 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.

Add Your ListView.builder() inside Expanded or Flex Widget hope its help to you

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

If Expanded or Flexible widgets didn't work, try giving a fixed width to your ListView items.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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