Search code examples
flutterdartflutter-layout

how to find the return value from the map is empty


im trying to create a listview with a search query, i want to show a empty icon when the return element form the map function is empty can any help me to achive this

ListView(
          children: controller.todo.value.where((element) {
        if (controller.search.value != '') {
          return element.labels!
                  .toString()
                  .toLowerCase()
                  .contains(controller.search.value);
        }
        return true;
      }).map((todo) {

      //here i need to check whether it is null or nor
      
      return Text(todo.name)}).toList())
           
            

Solution

  • Try this:

    Builder(
      builder: (context) {
        List<Widget> children =
            controller.todo.value.where((element) {
          if (controller.search.value != '') {
            return element.labels!
                .toString()
                .toLowerCase()
                .contains(controller.search.value);
          }
          return true;
        }).map((todo) {
          return Text(todo.name);
        }).toList();
    
        return children.isEmpty
            ? Center(
                child: Icon(Icons.warning),
              )
            : ListView(
                children: children,
              );
      },
    ),