I am learning to use StateNotifier and StateNotifierProvider from a YouTube video.
I have created a model and a StateNotifier class
User data model:
class Users {
final String? agentId;
final String? fName;
final String? lName;
final String? agency;
final String? agencyId;
final String? address1;
final String? address2;
final String? city;
final String? state;
final int? zipcode;
final String? cellPhone;
final String? officePhone;
final String? email;
final String? mls;
final String? mlsId;
Users(
{this.cellPhone,
this.address1,
this.address2,
this.agency,
this.agencyId,
this.agentId,
this.city,
this.fName,
this.lName,
this.officePhone,
this.state,
this.zipcode,
this.mls,
this.mlsId,
this.email});
Map<String, dynamic> toMap() {
return {
'cellPhone': cellPhone,
'address1': address1,
'address2': address2,
'agency': agency,
'agencyId': agencyId,
'agentId': agentId,
'city': city,
'fName': fName,
'lName': lName,
'officePhone': officePhone,
'state': state,
'zipCode': zipcode,
'mls': mls,
'mlsId': mlsId,
'email': email
};
}
// pass in a map and get an object back
Users.fromFirestore(Map<String, dynamic> firestore)
: cellPhone = firestore['cellPhone'],
address1 = firestore['address1'],
address2 = firestore['address2'],
agency = firestore['agency'],
agencyId = firestore['agencyId'],
agentId = firestore['agentId'],
city = firestore['city'],
fName = firestore['fName'],
lName = firestore['lName'],
officePhone = firestore['officePhone'],
state = firestore['state'],
zipcode = firestore['zipCode'],
mls = firestore['mls'],
mlsId = firestore['mlsId'],
email = firestore['email'];
}
StateNotifier class:
class UserNotifier extends StateNotifier<Users> {
UserNotifier(super.state); // constructor
// **************************************************
// functions to update class members
void updatefName(String newfName) {
state = state.copyWith(fName: newfName);
}
void updatelName(String newlName) {
state = state.copyWith(lName: newlName);
}
void updateaddress1(String newaddress1) {
state = state.copyWith(address1: newaddress1);
}
void updateaddress2(String newaddress2) {
state = state.copyWith(address2: newaddress2);
}
void updateCity(String newCity) {
state = state.copyWith(city: newCity);
}
void updateState(String newState) {
state = state.copyWith(state: newState);
}
void updateZipcode(String newZipcode) {
state = state.copyWith(zipcode: newZipcode);
}
void updateCellPhone(String newCellPhone) {
state = state.copyWith(cellPhone: newCellPhone);
}
void updateOfficePhone(String newOfficePhone) {
state = state.copyWith(officePhone: newOfficePhone);
}
void updateCompany(String newCompany) {
state = state.copyWith(company: newCompany);
}
void updateMls(String newMls) {
state = state.copyWith(mls: newMls);
}
}
I am getting an error on the copyWith. Here is the error message:
The method ‘copyWith’ isn’t defined for the type ‘Users’.
I searched on the internet and one post said I don’t need .copyWith so I removed it and I got a different error on state.
I have followed the example in the video but I am getting the error. How do I get rid of the error?
Thank you for your help.
>Solution :
In your Users model you don’t have copyWith method implemented which you are using in the statenotifier class.
add the following copyWith method in your Users model.
// Define a copyWith method
Users copyWith({
String? cellPhone,
String? address1,
String? address2,
String? agency,
String? agencyId,
String? agentId,
String? city,
String? fName,
String? lName,
String? officePhone,
String? state,
int? zipcode,
String? mls,
String? mlsId,
String? email,
}) {
return Users(
cellPhone: cellPhone ?? this.cellPhone,
address1: address1 ?? this.address1,
address2: address2 ?? this.address2,
agency: agency ?? this.agency,
agencyId: agencyId ?? this.agencyId,
agentId: agentId ?? this.agentId,
city: city ?? this.city,
fName: fName ?? this.fName,
lName: lName ?? this.lName,
officePhone: officePhone ?? this.officePhone,
state: state ?? this.state,
zipcode: zipcode ?? this.zipcode,
mls: mls ?? this.mls,
mlsId: mlsId ?? this.mlsId,
email: email ?? this.email,
);
}
Also, please edit the question to include the video link you are following.