Search code examples
stringflutterfunctionlistview

how can i get the String value of the title from the ListTile which was pressed by the user?


I need the String value of the tapped title from the ListTile

here is my code from the Listviewbuilder:

Expanded(
        child: ListView.builder(
          itemCount: displayed_add_ingridients_to_meal.length,
          itemBuilder: (context, index) => ListTile(
            onTap: Ingridients_Selected_x(),
            title: Text(displayed_add_ingridients_to_meal[index].Ingridient_title!),
          )
         )
         )

IngridientsSelected_x is a function in which I want to give the searched String value

I guess u have to call some argument in the onTap: method


Solution

  • Create your function with parameters of String title.

    Ingridients_Selected_x(String title){
      //
    }
    

    then invoke your function on tap and pass the title value to it.

     onTap:(){
          Ingridients_Selected_x(displayed_add_ingridients_to_meal[index].Ingridient_title!)
     }