skip to Main Content

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

2

Answers


  1. Just accept the String that you want to pass into your function:

    void Ingridients_Selected_x(String ingredientTitle) {
      // ... Your code...
    }
    

    And in your Listview.builder use an anonymous () => function :

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

    Or, to make it simpler, without the anonymous function:

    Expanded(
      child: ListView.builder(
        itemCount: displayed_add_ingridients_to_meal.length,
        itemBuilder: (context, index) => ListTile(
          onTap: () {
            Ingridients_Selected_x(displayed_add_ingridients_to_meal[index].Ingridient_title!);
          },
          title: Text(displayed_add_ingridients_to_meal[index].Ingridient_title!),
        ),
      ),
    )
    
    
    Login or Signup to reply.
  2. 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!)
     }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search