Home > OS >  How can I remove the icon when the validation is null in flutter?
How can I remove the icon when the validation is null in flutter?

Time:05-24

I have a function that receives a list in the function parameter and I am displaying that list.

I have a validation in the Text, when it is null it shows me a message that says "no data" or otherwise it shows me the value of the list.

What I want to remove the cancel icon when it is null and only appear when I have a value to display.

Help would be greatly appreciated.

Code and Image ::

Widget SfuluilderSuggestions(List <SDM> historialMenuPrincipal){
    return Container(

      child:StatefulBuilder(

        builder:(context,setState)
    {
return Container(

  child: ListView.builder(

      itemCount: historialMenuPrincipal.length,  
      itemBuilder: (context,i)
      {
        contentPadding: EdgeInsets.symmetric(vertical: 12,horizontal: 16);
        leading:CircleAvatar(
          radius: 32,
          backgroundImage: NetworkImage(
              "https://2.bp.blogspot.com/-3ZzNt8ZjQk/WR9W4Fn4II/AAAAAAAJw/_inTVynhS6V75IZ-461-pda7WyrTStwCEw/s1600/A.jpg"),
        );
        return
          ListTile(
           title: Text(historialMenuPrincipal[i] == null ? "":historialMenuPrincipal[i].email ), 
        trailing: IconButton(
              icon: Icon(Icons.cancel,color: Colors.black,),
              onPressed: () {
                  setState(() {
                    historialMenuPrincipal.remove(historialMenuPrincipal[i]);
                                   
                                          });
              },
            ),
            
          );
      }
  ),

);
    }
      )
    );

}

image

CodePudding user response:

You can check if the text is null -

trailing: Text(historialMenuPrincipal[i] != null ? IconButton(
          icon: Icon(Icons.cancel,color: Colors.black,),
          onPressed: () {
              setState(() {
                  historialMenuPrincipal.remove(historialMenuPrincipal[i]);
              });
           },
        ) : Padding(padding:EdgeInsets.zero),

CodePudding user response:

While historialMenuPrincipal contains data, you can remove only when data is available. You can pass null on trailing.

trailing:
    historialMenuPrincipal.contains(historialMenuPrincipal[i])
        ? IconButton(...)
        : null

If you want to skip the generating ListTile, you can check on return level and provide SizedBox or better filter data while generating the list.

  • Related