4

I get data from json but when try add data to list ant then print list get Instance of

class User {
  final String name;

  User(this.name);
}
....................
Map<String, dynamic> myMap = jsonData;
List<dynamic> lst = myMap["items"];
   for(var u in lst ){
       User user = User(u['name']);
       print(u['name']); //return name eg. Marc
       users.add(user);
  }

print(users);  //return Instance of

1 Answer 1

12

Override the toString() in your User class.

Something like this

class User {
  final String name;

  User(this.name);

  @override toString() => 'User: $name';
}

and then you can use

void main() {
  var users = [User("a"), User("b"), User("c")];
  print(users);

  print('----------------');

  for (var user in users) print(user);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.