Vishali
Vishali

Reputation: 1397

Flutter listview is not visible

 Widget servicesListview() {
    return Container(
        decoration: new BoxDecoration(color: const Color(0xFFEAEAEA)),
        child: Column(
          children: <Widget>[
            ListView.builder(
                scrollDirection: Axis.vertical,
                itemCount: menServicesList.length,
                itemBuilder: (BuildContext context, int index) {
                  Text(menServicesList[index].name);
                }),
          ],
        ));
  }

I am implementing listview in my flutter project whenever i call this method list is not visible.The page becomes blank,help me to solve this

Upvotes: 12

Views: 18051

Answers (4)

Darlan Dieterich
Darlan Dieterich

Reputation: 2537

I have a same problem. The ListView.builder don't work inside a Column, this necessary use the Expanded. The Expanded widget allows you to expand and fill the space with the relative widget.

See this:

child: Column(
   children: <Widget>[
      Expanded(
         child: ListView.builder(
            .......
         )
      )
   ]
)

Upvotes: 33

FlutterDevs
FlutterDevs

Reputation: 479

If you remove Column widget from the ListView, the list will definitely appear and if you want to give a decoration property to each element of a ListView.

Widget servicesListview() {
  return ListView.builder(
      scrollDirection: Axis.vertical,
      itemCount: menServicesList.length,
      itemBuilder: (BuildContext context, int index) {
        Container(
            decoration: new BoxDecoration(color: const Color(0xFFEAEAEA)),
            child: Text(menServicesList[index].name)
        );
      })
}

Upvotes: 2

Fellipe Malta
Fellipe Malta

Reputation: 3510

Wrap your ListView inside of a Expanded or a Container widget, if you use a Container, you'll need to set a height/width.

This happens because ListView doesn't have a height/width property to edit.

EDIT

Don't forget to return your Text inside the itemBuilder property.

Upvotes: 9

Jordan Davies
Jordan Davies

Reputation: 10861

You're missing the return statement in your itemBuilder

itemBuilder: (BuildContext context, int index) {
    return Text(menServicesList[index].name);
}),

or

itemBuilder: (BuildContext context, int index) => Text(menServicesList[index].name)),

Upvotes: 7

Related Questions