I want to add a ListView with elements. I another page I do it, but for some reason now its doesn't appear.

Widget comentCard(comentario) {
  return Container(
    width: 300,
    padding: EdgeInsets.symmetric(horizontal: 3),
    child: Card(
      elevation: 1,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.all(
          Radius.circular(30),
        ),
      ),
      child: Text('Some Text'),
    ),
  );
}

Widget comentarios() {
  return ListView(
    children: widget.model.cardComentarios.map((element) {
      for (var i = 0; i < widget.model.cardComentarios.length; i++) {
        return comentCard(element);
      }
    }).toList());
}



@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      automaticallyImplyLeading: true,
      title: Text('Title'),
      actions: [], // agregar boton a la derecha
    ),
    body: Container(child: comentarios()),
  );
}

If I click in another buttom, sometimes appears, but I want that appears when the page is loaded.

Thanks!

1 Answers

0
Hugo Passos On Best Solutions

Your comentarios method is wrong. A map already iterates for you, so you're iterating twice.

A map is supposed to take a list of type A and map it into another list of type B. Check the example:

var list = [1, 2, 3].map((int number) => number.toString());

print(list);  // Output: ['1', '2', '3']

Your comentarios method must be either one of these:

Widget comentarios() {
  List comentWidgetList;

  for (var i = 0; i < widget.model.cardComentarios.length; i++) {
    var comentCard = comentCard(widget.model.cardComentarios[i]);
    comentWidgetList.add(comentCard);
  }

  return ListView(
    children: comentWidgetList,
  );
}
Widget comentarios() {
  return ListView(
    children: widget.model.cardComentarios.map((element) {
      return comentCard(element);
    }).toList(),
  );
}