how to get the variable from my list in flutter

Issue

when i want to select from my list i don’t get the variable just this msg : Instance of Vocabulary.

color: (_selectedItems.contains(HomePage.vocabularyList[index])) ? Colors.blue.withOpacity(0.5) : Colors.transparent,
                   //select and import to a category
                      child: ListTile(
                        //to remove
                        onTap: (){
                        if(_selectedItems.contains(HomePage.vocabularyList[index])){
                        setState(() {
                        _selectedItems.removeWhere((val) => val == HomePage.vocabularyList[index]);
                        });}},
                        //to add
                        onLongPress: (){
                         //print(HomePage.vocabularyList[index].CategoryId);
                        if(! _selectedItems.contains(HomePage.vocabularyList[index])){
                        setState(() {
                        _selectedItems.add(HomePage.vocabularyList[index]);
                        print(HomePage.vocabularyList[index]);
                        });}
                        },
                        title: _showWords(index),
              ),);

and output is:

I/flutter (  338): Instance of 'Vocabulary'

Solution

It’s because your Vocabulary class doesn’t have a .toString() method.
You can try to display one of the properties of your class by using

print(Homepage.vocabularyList.elementAt(index).word);

Where word is String property of your Vocabulary class

Answered By – BLKKKBVSIK

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *