I am trying to pass a few basic strings to a User Model and then on set the strings are trimmed, but my issue is if I make the passed variables to User #required and using the usual way of using set and get give me the error " Named optional parameters can't start with an underscore"
class User {
String id;
String lastName;
String email;
String _firstName;
String get firstName => _firstName;
set firstName(String name) => _firstName = name.trim();
User({#required this.id,
#required this._firstName, // Error thrown here
#required this.lastName,
#required this.email});
}
If I do the following I get static analysis errors on the set and get variables saying The declaration '_firstName' isn't referenced. Try removing the declaration of '_firstName'
class User {
String id;
String lastName;
String email;
String firstName;
String get _firstName => firstName; // Static analysis error here
set _firstName(String name) => _firstName = name.trim(); // Static analysis error here
User({#required this.id,
#required this.firstName,
#required this.lastName,
#required this.email});
}
Am I using get and set wrong? Is there a better way to do this?
EDIT : In my opinion, in your case, there is no need to use getters and setters (see documentation :
class User {
String id;
String lastName;
String email;
String firstName;
User({
#required this.id,
#required this.firstName,
#required this.lastName,
#required this.email,
}) {
firstName = firstName.trim();
}
}
Usage :
final user = User(
id: '1',
firstName: 'John',
lastName: 'Doe',
email: 'john#doe.com',
);
// set firstName :
user.firstName = 'Jane';
// get firstName :
print(user.firstName);
With getters and setters you expose a way to either get/set a value to local/private object.
Something like,
String get firstname => _firstName;
set firstName => _firstName = name.trim();
From the code you've shown, if all you're trying to do, is return a trimmed value, you can do that with only getter. Your code in that case might look as follows.
class User {
String id;
String email;
final String name;
String get firstName => name.trim();
String get lastName => name.trimLeft(); //some other calculation
User({#required this.id,
#required this.name,
#required this.email});
}
Related
Hi I'm new to dart and flutter and I want to create a method that update multiple fields at once.
For example, suppose there is a class named User, and it looks like this:
class User {
int id;
String password;
String firstName;
String lastName;
String nickName;
String gender;
DateTime birthday;
String phoneNumber;
String address;
...
}
In this example, an instance of User will have a lot of fields and it's awkward to update multiple fields if you don't intend to update all of them.
So, when you update only password, nickName, phoneNumber and address, instead of reassigning a new User instance like this:
user = User(
id : 0,
password : 'xxxxxxx',
firstName : 'Hanako',
lastName : 'Tanaka',
nickName : 'Tanako',
gender : 'female',
birthday : DateTime(2000, 1, 1),
phoneNumber : 'xxxxxxxxxxx',
address : 'xxxxxxxxxxx'
);
I want to update them like this:
user.updateUser({
password : 'xxxxxx',
nickName : 'Tanako',
phoneNumber : 'xxxxxxxxxxx',
address : 'xxxxxxxxxxx'
});
Please tell me if there is a way to create a method that update multiple fields at once like this.
Thanks,
you can do this like this
class User {
int id;
String password;
String firstName;
String lastName;
String nickName;
String gender;
DateTime birthday;
String phoneNumber;
String address;
User({
required this.id,
required this.password,
required this.firstName,
required this.lastName,
required this.nickName,
required this.gender,
required this.birthday,
required this.phoneNumber,
required this.address,
});
updateUser({
int? id,
String? password,
String? firstName,
String? lastName,
String? nickName,
String? gender,
DateTime? birthday,
String? phoneNumber,
String? address,
}) {
this.id = id ?? this.id;
this.password = password ?? this.password;
this.firstName = firstName ?? this.firstName;
this.lastName = lastName ?? this.lastName;
this.nickName = nickName ?? this.nickName;
this.gender = gender ?? this.gender;
this.birthday = birthday ?? this.birthday;
this.phoneNumber = phoneNumber ?? this.phoneNumber;
this.address = address ?? this.address;
}
}
when you don't give a property name it will not update that field of the object for example if you want to update only firstName and lastName you can do it like this
user.updateUser(firstName:"Nao",lastName:"COMATSU");
and all other fields will not be updated
Hello I explain my problem I have a list of patients with their coordinates all that is registered in a class by clicking on one of the patient a life opens with the information of the patient in question.
I want to add the possibility to add notes (an unlimited number of notes per patient) so I need another list but I do not know how to link the two
I would like that when I click on a patient it loads the information of this patient from the list of notes
Here is the list of my patients
class Patients {
final String name;
final String firstname;
final String dateofbirth;
final String email;
final String numero;
final String id;
final DateTime date;
Patients(
{required this.name,
required this.firstname,
required this.dateofbirth,
required this.email,
required this.numero,
required this.id,
required this.date});
}
Here is the list of patient notes
class ListNote {
final String? title;
final String? note;
final String? conclusion;
ListNote({
this.title,
this.note,
this.conclusion,
});
}
My patient screen list
In the red frame is the information of my first list of patients.
In the blue boxes you will find the notes linked to the patient
Patient page with more info
Thank you for your help
What I got from your point is you want to add List of Notes in Patient class. If that's the case you can simply add that in Patient class like this:
class Patients {
final String name;
final List<ListNote> listOfNotes;
final String firstname;
final String dateofbirth;
final String email;
final String numero;
final String id;
final DateTime date;
Patients({
required this.name,
required this.listOfNotes,
required this.firstname,
required this.dateofbirth,
required this.email,
required this.numero,
required this.id,
required this.date
});
}
class ListNote {
final String? title;
final String? note;
final String? conclusion;
ListNote({
this.title,
this.note,
this.conclusion,
});
}
class Profile {
final String firstName;
final String lastName;
final session = getIt<Session>();
Profile({
this.firstName = session.profile.firstName,
this.lastName = session.profile.lastName
});
}
This code has error says
The default value of an optional parameter must be constant
So how can I initialize class that has optional parameter and default value from other instance?
Try to use the initialization list or the member initializer instead of initializing the attributes inside the constructor
For example:
class Profile {
final String? firstName;
final String? lastName;
final session = getIt<Session>();
Profile({
this.firstName,
this.lastName,
})
: firstName = firstName ?? session.profile.firstName,
lastName = lastName ?? session.profile.lastName;
}
I write this class
class Profile {
String email;
String password;
Profile({this.email, this.password});
}
But it said that
"The parameter 'email' can't have a value of 'null' because of its type, but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.dartmissing_default_value_for_parameter"
By adding required:
class Profile {
String email;
String password;
Profile({
required this.email,
required this.password,
});
}
By adding optional:
class Profile {
String? email;
String? password;
Profile({
this.email,
this.password,
});
}
By adding default values:
class Profile {
String email;
String password;
Profile({
this.email = "mail#gmail.com",
this.password = "123",
});
}
You have to make them accept a null value.
class Profile {
String? email;
String? password;
Profile({this.email, this.password});
}
If I have this class :
class Person {
Person(
#required this.name,
#required this.age,
#required this.job,
});
final String name;
final int age;
final String job;
);
How can I create an object that has all these properties but set by me before the use? Something like :
Person william(name : 'William', age : 25, job : 'officer');
doSomething(william);
Please see the code below :
void main() {
final Person william = Person(name:"William", age:25, job:"officer");
doSomething(william);
}
void doSomething(Person person) {
print('${person.name} ${person.age} ${person.job}');
}
class Person {
Person({
this.name,
this.age,
this.job,
});
final String name;
final int age;
final String job;
}