in return only show Instance of 'User' User is a class - flutter

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,octavia#gmail.com, 099-83-44 \n lark, clark#gmail.com, 054-83-23 \n aven, raven#gmail.com, 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;
}

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

Why Last value of a class object String assign to first one object in flutter

Click Here to see Dartpad Screenshot
void main(){
Student file1 = Student.empty;
Student file2 = Student.empty;
file1.name = 'ABC';
file2.name = 'DEF';
print(file1.name);
print(file2.name);
}
class Student{
String name;
Student({
required this.name,
});
static Student empty = Student(name: '');
}
Output Value
DEF
DEF
Expected Value
ABC
DEF
This happens, because you are using the same static instance of Student, since the static field is shared across all instances of Student.
So your variables file1 and file2 are referencing the same single instance of Student.
You may want to use a factory constructor instead:
https://dart.dev/guides/language/language-tour#factory-constructors
void main() {
Student file1 = Student.empty();
Student file2 = Student.empty();
file1.name = 'ABC';
file2.name = 'DEF';
print(file1.name);
print(file2.name);
}
class Student {
String name;
Student({
required this.name,
});
factory Student.empty() {
return Student(name: '');
}
}

Mapping CSV data in flutter

Auto-complete search list
How to parse csv data instead of json data as mentioned in this article. I am new to csv and I have trouble mappping csv data to a model list. I need to pass the csv list to autocomplete field in another package plz help me in mapping it to the model.
class Players {
String keyword;
int id;
String autocompleteterm;
String country;
Players({
this.keyword,
this.id,
this.autocompleteterm,
this.country
});
factory Players.fromJson(Map<String, dynamic> parsedJson) {
return Players(
keyword: parsedJson['keyword'] as String,
id: parsedJson['id'],
autocompleteterm: parsedJson['autocompleteTerm'] as String,
country: parsedJson['country'] as String
);
}
}
class PlayersViewModel {
static List<Players> players;
static Future loadPlayers() async {
try {
players = new List<Players>();
String jsonString = await rootBundle.loadString('assets/players.json');
Map parsedJson = json.decode(jsonString);
var categoryJson = parsedJson['players'] as List;
for (int i = 0; i < categoryJson.length; i++) {
players.add(new Players.fromJson(categoryJson[i]));
}
} catch (e) {
print(e);
}
}

Flutter: Transferring items from one list into a different list

i have one List (growable) with an item (actually item 0:
items is of class Team
items[_id = 1, _team = "Team01", _note = "blabla"]
and I want to transfer it into another list with a different structure:
participants is of class User
participants[id = 1, name = "participant1"]
skipping the note and translating _id into id and so on.So at last the result would give me
participants[id = 1, name = "team01"]
(sorry for the writing, I describe it out of the debugger)
i tried something like this, but doesnt work with value:
List<TestTeam> participants;
for (var value in items) {
participants.add(new TestTeam(value.id, value.team));
}
my class Team is defined like this:
class Team {
int _id;
String _team;
String _note;
Team(this._team, this._note);
Team.map(dynamic obj) {
this._id = obj['id'];
this._team = obj['team'];
this._note = obj['note'];
}
int get id => _id;
String get team => _team;
String get note => _note;
Map<String, dynamic> toMap() {
var map = new Map<String, dynamic>();
if (_id != null) {
map['id'] = _id;
}
map['team'] = _team;
map['note'] = _note;
return map;
}
Team.fromMap(Map<String, dynamic> map) {
this._id = map['id'];
this._team = map['team'];
this._note = map['note'];
}
}
You should implement below way
void main() {
List<Team> teams=[];
List<User> participants=[];
for (var i = 0; i < 4; i++) {
teams.add(Team(i,'Team_$i','Note_$i'));
}
for (var value in teams){
participants.add(User(value.id,value.team));
}
for (var value in teams){
print(value.toString());
}
for (var value in participants){
print(value.toString());
}
}
class Team{
int id;
String team;
String note;
Team(this.id,this.team,this.note);
toString()=> 'Team Map :{id:$id,team:$team,note:$note}';
}
class User{
int id;
String team;
User(this.id,this.team);
toString()=> 'User Map :{id:$id,team:$team}';
}
Output
Team Map :{id:0,team:Team_0,note:Note_0}
Team Map :{id:1,team:Team_1,note:Note_1}
Team Map :{id:2,team:Team_2,note:Note_2}
Team Map :{id:3,team:Team_3,note:Note_3}
User Map :{id:0,team:Team_0}
User Map :{id:1,team:Team_1}
User Map :{id:2,team:Team_2}
User Map :{id:3,team:Team_3}

how to make select in list of objects

I´d like filter this list to obtain adress(result should be 'address 002') where name == 'name02'.
How can I achieve this?
And also not sure where is the best place to insert data into list.I only need insert data into list at once, when application starting.
class ListOfAdressDb {
String name;
String address;
double locLat;
double locLng;
ListOfAdressDb({this.name, this.address, this.locLat, this.locLng});
#override
String toString() {
return '{ ${this.name}, ${this.address}, ${this.locLat}, ${this.locLng} }';
}
}
main() {
List listOfAdress = [];
listOfAdress.add(ListOfAdressDb(
name: 'name01',
address: 'address 001',
locLat: 11.1111111,
locLng: 11.1111111));
listOfAdress.add(ListOfAdressDb(
name: 'name02',
address: 'address 002',
locLat: 22.2222222,
locLng: 22.2222222));
}
You can filter your List with the where-funtion of List. This function will give you an filtered List like that:
var filteredList = listOfAddress.where((ListOfAddressDB entry) => entry.name == 'name02').toList();

Dart model throws error when mixing many data types

I made this simple model to work with an API
class BooksModel {
List<_Book> _books = [];
BooksModel.fromJson(Map<dynamic, dynamic> parsedJson) {
List<_Book> temp = [];
for (int i = 0; i < parsedJson['books'].length; i++) {
_Book book = _Book(parsedJson['books'][i]);
temp.add(book);
}
_books = temp;
}
List<_Book> get books => _books;
}
class _Book {
int _id;
String _name;
_Book(book) {
_id = book['id'];
_name = book['name'];
}
int get id => _id;
int get name => _name;
}
The problem is i have to turn all '_Book' class properties to 'String', if i made only one 'int' as given in the above example, it throws this error.
type 'String' is not a subtype of type 'int'
I don't even use that 'id' which is 'int' in my app, so it's not about the usage of it, the problem is in this model
Can you just show the example of your json, so that i can tell you that what your model should be or where it is going wrong.
So maybe your issue is not that big , id required is integer and you are passing the String.
Is book['id'] a string?
Try it:
// _id = book['id'];
_id = int.parse(book['id']);