1

how to create a horizontal listview in which data is taken from dart object classes i.e API

this is the code:

return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("practice"),
        ),
        // child: Text("data")
        body:Container(
           child: ListView.builder(
           scrollDirection: Axis.horizontal,
           itemCount: l1.length,
           itemBuilder: (context , index){
              return ListTile(
                     title: Image.network(l1[index]),
                     subtitle: Text(l2[index]),
               );

i am getting this error:

RenderBox was not laid out: RenderPointerListener#784b4 relayoutBoundary=up8 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
'package:flutter/src/rendering/box.dart':
Failed assertion: line 1694 pos 12: 'hasSize'
  • https://stackoverflow.com/questions/52801201/flutter-renderbox-was-not-laid-out refer this, add couple of information 1. code that assigns values to l1 and l2 variable. 2. the complete error stack. – MeanMan Jun 08 '20 at 05:23
  • In case if l1 and l2 are populated using api calls, I would suggest looking into StreamBuilder and FutureBuilder too. – MeanMan Jun 08 '20 at 05:23

2 Answers2

1

Your list view should be wrapped inside a container that has a height, adding height to your container may fix this

body:Container(
       height: 100,
       child: ListView.builder(
       scrollDirection: Axis.horizontal,
       //Rest of your code
Vilsad P P
  • 1,529
  • 14
  • 23
0

Solved for me . your ListTile need width size . see example and change your code.

example code:

   body: ListView.builder(
        scrollDirection: Axis.horizontal,
        itemCount: [1,2,3].length,
        itemBuilder: (context, index) {
          return SizedBox(
            height: 200, 
            width: 500, // <---- mandatory size
            child: ListTile(
              subtitle: Text("${[index]}"),
            ),
          );
        })
Esmaeil Ahmadipour
  • 840
  • 1
  • 11
  • 32