How come in dart I can't do this?
abstract class IUserDto {
String id;
String firstName;
String lastName;
String email;
}
class UserLogicItem implements IUserDto {
UserLogicItem({this.id, this.firstName, this.lastName, this.email});
#override
final String id;
#override
final String firstName;
#override
final String lastName;
#override
final String email;
}
I get this error:
Missing concrete implementations of 'setter IUserDto.email', 'setter
IUserDto.firstName', 'setter IUserDto.id', and 'setter
IUserDto.lastName'. Try implementing the missing methods, or make the
class abstract.dart(non_abstract_class_inherits_abstract_member)
But I want to create a class contract where the concrete class has to have those properties.
class UserLogicItem implements IUserDto {
UserLogicItem({this.id, this.firstName, this.lastName, this.email});
#override
String email;
#override
String firstName;
#override
String id;
#override
String lastName;
}
Add the missing setters in the UserLogicItem and it'll resolve this issue. You can press Ctrl + Enter to quickly add the missing overrides or use Ctrl + O from the class to check missing overrides.
class UserLogicItem implements IUserDto {
UserLogicItem({this.id, this.firstName, this.lastName, this.email});
#override
final String id;
#override
final String firstName;
#override
final String lastName;
#override
final String email;
#override
void set email(String _email) {
// TODO: implement email
}
#override
void set firstName(String _firstName) {
// TODO: implement firstName
}
#override
void set id(String _id) {
// TODO: implement id
}
#override
void set lastName(String _lastName) {
// TODO: implement lastName
}
}
Related
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,
});
}
I want to create an empty HiveList for my Hive class to make a relation with another Hive class. But I cannot make a default value at the constructor. I think I am doing this wrong. Unfortunately the Hive docs does not cover this issue.
#HiveType(typeId: 0)
class Account extends HiveObject {
#HiveField(0)
String name;
#HiveField(1)
int amount;
#HiveField(2)
HiveList<History> history; // Want to make this list
Account({
required this.name,
required this.amount,
this.history = [] as HiveList<History>, // Error Here
});
}
You should be able to do something like this:
#HiveType(typeId: 0)
class Account extends HiveObject {
#HiveField(0)
String name;
#HiveField(1)
int amount;
#HiveField(2)
HiveList<History> history;
Account({
required this.name,
required this.amount,
this.history = const HiveList<History>.empty(),
});
}
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;
}
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;
}
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});
}