I have a list of names.
I have a class with name details like name, age, and height.
I need to create a list of names and add to the list all the details.
I am not seeing any data. Instead, I see: mylist is instance of.
Could you please advise how to address this issue.
Thank You
My code:
PeopleList=[Ana, Ron,Jack, Tom, Lisa, Frank];
class myPerson {
String name;
int age;
int height;
myPerson({required this.name, required this.age, required this.height});
}
List<myPerson> mypeople = [];
Future<List<myPerson>> getPeople() async {
for(var p in await PeopleList()) {
if(p.isNotEmpty){
mypeople.add(MyPerson(name: p.name,age:p.age , height:p.height);
}
}
return mypeople;
}
2
Answers
When you print an object, Flutter calls the
toString()
method on that object. And since you don’t implementtoString
in yourmyPerson
class, you end up with the defaultObject.toString()
implementation, which indeed showsinstanceOf <whatEverClassTheObjectIsOf>
.If you want it to print something different, override
toString()
in yourmyPerson
class.Also see, this Dart tutorial on overriding methods, which uses
toString
in its example.If you want to print out the values of the properties in your class (myPerson), you will have to specify them in your print method.
For example:
Or you can override the toString method in your class (myPerson) to print out the values of the properties.
Then you can print the list of people like this: