I am getting an error while adding scrollDirection: Axis.horizontal
to the ListView
builder of my ListView
.
The application crashes.
return Scaffold(
appBar: AppBar(),
body: _users == null || _users!.isEmpty
? const Center(
child: CircularProgressIndicator(),
)
: ListView.builder(
physics: ScrollPhysics(),
scrollDirection: Axis.horizontal,
shrinkWrap: true,
primary: false,
itemCount: _users.length,
itemBuilder: (BuildContext ctxt, int i) {
return new Card(
child: Column(
children: [
Text(_users[i].name.toString()),
ListView.builder(
physics: ScrollPhysics(),
shrinkWrap: true,
primary: false,
itemCount: _users[i].chart.length,
itemBuilder: (BuildContext ctx, int j) {
return Text(_users[i]
.chart[j]
.ch_timestamp); // display username as an example
},
),
],
),
);
},
),
);
2
Answers
Try wrapping your inner
ListView
with aSizedBox
that haswidth
specified.The problem here is your inner vertical
ListView
tries to take all the availablewidth
which is not possible because the parentListView
scrolls horizontally, which means it has infinitewidth
.The error you have is that you are trying to add horizontal scrolling to a
ListView
that its default is vertical. to be able to use horizontal scroll you should usescrollDirection
toAxis.horizontal
. and your crashing issue is related to that you are addingListView
insideListView.builder
but you can useRow
and horizontally scrolling instead:happy coding…