我正在通过 YouTube 视频学习使用 StateNotifier 和 StateNotifierProvider。
我创建了一个模型和一个 StateNotifier 类
用户数据模型:
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 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);
}
}
我在 copyWith 上遇到错误。这是错误消息: 未为“Users”类型定义“copyWith”方法。
我在互联网上搜索,有一篇文章说我不需要 .copyWith,所以我删除了它,并且在状态上出现了不同的错误。
我已按照视频中的示例进行操作,但出现错误。如何消除错误?
谢谢您的帮助。
在您的 Users 模型中,您没有实现在 statenotifier 类中使用的 copyWith 方法。
在您的 Users 模型中添加以下 copyWith 方法。
// 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,
);
}
此外,请编辑问题以包含您正在关注的视频链接。