Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView.builder doesn't show anything - Flutter

Tags:

flutter

dart

When I'm, not using the ListView.builder constructor in Flutter, the individual item is shown as expected from the JSON API:

when I use ListView.builder, nothing shows up. no Error!

I also tried a listview with Texts only that doesn't seem to work either. Here's the code:

@override
Widget build(BuildContext context) {

return Container(

 child: new Scaffold(
     appBar: AppBar(
         title: Text("the title"),//TODO edit this
         backgroundColor: Colors.blueAccent),

     body:
     Column(
       children: <Widget>[
         FutureBuilder<List<dynamic>>(
           future: getPosts2(),
           builder: (context, snapshot) {
             if (snapshot.hasError) print(snapshot.error);

             return snapshot.hasData
                 ? ListViewPosts(postsFrom: snapshot.data)
                 : Center(child: CircularProgressIndicator());
           },
         ),


       ],
     ),

 ),
  );
 }
 }

And here is the ListViewPosts Stateless Widget:

     class ListViewPosts extends 
     StatelessWidget {
     final List<dynamic> postsFrom;

     ListViewPosts({Key key, 
     this.postsFrom}) : super(key: key);

       @override
       Widget build(BuildContext context) {
return Column(
  children: <Widget>[
    FadeInImage.assetNetwork(
      placeholder: 'assets/images/placeholder.png',
      image: postsFrom[1]["featured_media"] == 0
          ? 'assets/images/placeholder.png'
          : postsFrom[1]["_embedded"]["wp:featuredmedia"]
      [0]["source_url"],


    ),

    FadeInImage.assetNetwork(
      placeholder: 'assets/images/placeholder.png',
      image: postsFrom[2]["featured_media"] == 0
          ? 'assets/images/placeholder.png'
          : postsFrom[2]["_embedded"]["wp:featuredmedia"]
      [0]["source_url"],


    ),
    new Row(
      children: <Widget>[
        Expanded(
          child: Text(
            "نووسه‌ر: " +
                postsFrom[1]["_embedded"]["author"][0]
                ["name"],
            textAlign: TextAlign.right,
          ),
        ),
        Expanded(
          child: Text(
            dateConvertor(
                postsFrom[2]["date"].toString()),
            textAlign: TextAlign.left,
          ),
        ),
      ],
    ),

    ListView.builder(
      itemCount: postsFrom.length, //== null ? 0 : postsFrom.length,
      itemBuilder: (context, int index) {
        Card(
          child: Column(
            children: <Widget>[
              Text(postsFrom.toString()),
              Container(
                child: hawalImage(postsFrom, index),
              ),
              new Padding(
                padding: EdgeInsets.all(5.0),
                child: new ListTile(

                  title: new Text("whatEver"),
                  subtitle: new Row(
                    children: <Widget>[
                      Expanded(
                        child: new Text(postsFrom[index]["title"]["rendered"]),
                      ),
                      Expanded(
                        child: hawalDate(postsFrom, index),
                      ),
                    ],
                  ),
                ),
              ),
              new ButtonTheme.bar(
                child: hawalBtnBar(),
              ),
            ],
          ),
        );
      },
    ),
like image 922
Hooshyar Avatar asked Jul 11 '26 17:07

Hooshyar


2 Answers

You have to write return Card at the beginning of the curly brackets in the builder function. Also I would be cautious with using Expanded there, it might cause some errors.

Also you put a ListView inside a Column without defining it's height, so it will take up space indefinitely. Wrap it in a widget that provides height constraints(SizedBox, Expanded, ...)

like image 178
leodriesch Avatar answered Jul 13 '26 08:07

leodriesch


Inside the Listview.builder() you could try to add the property shrinkWrap: true.

This worked for me. I was facing a similar issue.

like image 41
Ayrix Avatar answered Jul 13 '26 07:07

Ayrix