我可以在 if 条件下仅 return SliversList 中的 ListTile 吗?

Can i only return ListTile in SliversList under an if condition?

如果我想查看的用户在他的数据库部分有特定内容,我将尝试仅在 CustomScrollView 中 return 中的 ListTile 中的 SliversList 中。我怎样才能做到这一点?我想做这样的事情:

CustomScrollView(
  slivers: [
    SliverList(
        delegate: SliverChildListDelegate(snapshot.data!.docs
            .map((DocumentSnapshot document) {
      final data = document.data() as Map<String, dynamic>;
      String likesString = data["likes"].toString();
      List<String> likesList = likesString.split(' ');
      var likes = likesList.length - 1;
      
      if(data['favsub'].toString().contains("test"){
        return ListTile(
          onTap: () => callChatDetailScreen(
              context, data['name'], data['uid']),
          title: Text(data['name']),
          leading: GestureDetector(
            onTap: () {
              showDialog(
                  context: context,
                  builder: (context) => Dialog(
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.all(
                              Radius.circular(32.0))),
                      backgroundColor: Colors.transparent,
                      child: Container(
                          decoration: new BoxDecoration(
                              image: new DecorationImage(
                                  fit: BoxFit.scaleDown,
                                  image: new NetworkImage(
                                      data['url']))))));
            },
            child: Container(
              width: 100,
              height: 100,
              decoration: new BoxDecoration(
                  shape: BoxShape.circle,
                  image: new DecorationImage(
                      fit: BoxFit.scaleDown,
                      image: new NetworkImage(data['url']))),
            ),
          ),
          subtitle: Text(
            likes.toString() + " Likes",
            style: TextStyle(
                fontWeight: FontWeight.bold,
                color: getColor(likes)),
          ));
      }
      else{
        //do nothing
      }

      
    }).toList()))
  ],
),

SliverChildListDelegate需要位置参数,即List<Widget>。您可以 return 清空 [] 列表。

映射数据时,您可以移除else状态。它会工作正常。此外,您可以传递空 SizedBox() 或任何小部件来显示信息。