Home > Mobile >  How to print the value without selecting the index in Choice Chip in Flutter
How to print the value without selecting the index in Choice Chip in Flutter

Time:10-18

I am using chips_choice library and also fetching the api data, then this api data is displaying on chips_choice which is displaying is "Apple" & "Banana" and by default first index is selected that is "Apple". So my question is by default first index is selected how can i print that already selected index without clicking or selecting it manually? Below is the sample dart code.

String specialRequest = "";
Row(
children: [
GestureDetector(
onTap: (){
print(specialRequest); // I want to print "Apple" without selecting or clicking manually .
},
child: Text('What is the occasion')),
            Padding(
              padding: const EdgeInsets.only(left: 10.0),
              child: Container(
                width: MediaQuery.of(context).size.width * 0.45,
                child: ListView.builder(
                    shrinkWrap: true,
                    scrollDirection: Axis.horizontal,
                    itemCount: data.data!.occassions!.length,
                    itemBuilder: (BuildContext context, int index) {
return ChoiceChip(
label: Text(data.data!.occassions![index]!.name!), // Api data displaying is "Apple" "Banana"
                        selected: occasionIndex == index,
                        selectedColor: TuxedoColor.redColor,
                        onSelected: (bool selected) {
                          specialRequest = data.data!.occassions![index]!.id!;
                          setState(() {
                            occasionIndex = selected ? index : 0;
                          });
                        },
                        backgroundColor: Colors.white,
                        labelStyle: TextStyle(
                            color: Colors.black,
                            fontWeight: FontWeight.bold),
                      );
                    }),
              ),
            )
          ],
        ),

CodePudding user response:

If I understand well, you want to print the value or the index.

Change this:

                    itemBuilder: (BuildContext context, int index) {
                      return ChoiceChip(

To this:

                itemBuilder: (BuildContext context, int index) {
                  specialRequest = data.data!.occassions![index]!.id!; //***New Line ***
                  print(specialRequest);
                  return ChoiceChip(

Let me know if you meant something else

  • Related