Same item repeating in list while using listview.separated

时间:2018-12-03 13:07:15

标签: listview flutter flutter-layout divider

For some reason, the list I am trying to display using listView.separated is repeating every item, as shown in screenshot below:

enter image description here

Expected behavior is to display every item only once with a divider, hence I went for listView.separated.

enter image description here

Current code to display this list is:

Widget proTile(Pro pro, BuildContext context) {

        return ListView.separated(
          separatorBuilder: (pro, context) => Divider(
            color: Colors.black,
          ),
          shrinkWrap: true,
          physics: ScrollPhysics(),
          itemCount: pros.length,
          itemBuilder: (context, index) =>
          (GestureDetector(
          child: new Container(padding: EdgeInsets.all(10.0),
          child: new Row(
          children: <Widget>[
            _getProfilePic(pro),
            new Padding(
              padding: EdgeInsets.only(left: 9.0),
            ),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  new Text(
                    '${pro.fullname}',
                   style: new TextStyle(
                     fontSize: 16.0
                   ),
                  ),
                  new Padding(
                    padding: EdgeInsets.only(top:3.0),
                    child: new Text(
                    '${pro.company}',
                    style: new TextStyle(
                      fontSize: 12.0
                    ),
                  )
                  ),
                ],
              ),
            ),
            new Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                new StarRating(starCount: 5,rating: pro.rating,color: Colors.amber),
                new Padding(
                  padding: EdgeInsets.only(top: 3.0),
                  child: new Text(
                ('${pro.reviewCount} reviews'),
                style: new TextStyle(
                  fontSize: 12.0
                ),),
                ),
              ],
            ),
          ],
          ),
          ),
            // Go to the profile page of the pro
            onTap:()  => Navigator.push(context,
                MaterialPageRoute(builder: (context) => ProfilePage(pro, this.fireBaseUser, this.user))
            )
        )
          )
        );
      }

// Returns a list view of pro list tiles
  Widget buildProList(List<dynamic> pros, BuildContext context) {
    List<Widget> proTiles = new List<Widget>();

    pros.forEach((pro) {
      proTiles.add(proTile(pro, context));
    });

    return new ListView(
      children: proTiles
    );
  }

  @override
  Widget build(BuildContext context) {
    return new Center(
      child: buildProList(pros, context),
    );
  }

Looking for guidance here as where the problem could be and an appropriate solution on it.


data output :

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以只使用L'\0'方法,现在您正在为每个项目创建一个ListView.separated,这是不正确的,请尝试以下更改:

删除参数

proTile

更改构建方法

 Widget proTile(BuildContext context) {

在您的 @override Widget build(BuildContext context) { return new Center( child: proTile(context), ); } 方法内,不要使用粗箭头,因为您需要获取当前对象:

proTile