Home > Back-end >  how to get the variable from my list in flutter
how to get the variable from my list in flutter

Time:03-29

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'

CodePudding user response:

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

CodePudding user response:

In order to print an object you should override the toString() method. Sample code:

class Vocabulary{
  int id;
  String text;
  String language;
  Vocabulary(this.id,  this.text, this.language);

  @override
  String toString() {
    return 'Vocabulary{id: $id, text: $text, language: $language}';
  }
}
  • Related