0

I am trying to only return a ListTile in a SliversList in a CustomScrollView if the user I want to view has something specific in his database section. How can I achieve this? I want to make something like this:

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()))
  ],
),
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56

1 Answers1

0

SliverChildListDelegate requires a positional argument, and that is List<Widget>. You can return empty [] list.

While mapping data, you can remove else state. It will work fine. Also, you can pass empty SizedBox() or any widget to show information.

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
  • Thank you, but if i delete the else state and only return the listtile in an if state, then i get the error: The argument type 'List' can't be assigned to the parameter type 'List' – Studdle TBFG Mar 08 '22 at 07:32
  • `'List` mean nullable widgets, if you sure you won't add nullable widget you can use `!` or change to `List` or `List` – Md. Yeasin Sheikh Mar 08 '22 at 07:52