Home > other >  in return only show Instance of 'User' User is a class
in return only show Instance of 'User' User is a class

Time:12-16

I want this code to convert a csv file to a list, and then convert it to json, for the firebase database, instead of list this code return array it shows an instance of class, like this :

[Instance of 'User', Instance of 'User', Instance of 'User'] 
void main() {
 var  users= "username, email, phone \n ctavia,[email protected], 099-83-44 \n lark, [email protected], 054-83-23 \n aven, [email protected], 784-44-98";
 var data = csvToUsersList(users);
   
      print(data); 
  
}

class User {
        String username;
        String email;
        String phone;
    
      User(this.username, this.email, this.phone);
    }


List<User> csvToUsersList(String data) {
  List<User> users = [];
  
  List<String> userin= data.split("\n");

  for (int i = 1; i < userin.length; i  ) {
    List<String> user = userin[i].split(",");
    
    users.add(User(user[0], user[1], user[2]));
    
  }

  return users;
}

CodePudding user response:

That seems correct. If you print something like data.first.username, you should get the name of the first User.

Instance of User just means, that this is an Object of Type User.

  • Related