我有一个povider类,我正在转换到riverpod 2.0。我已经定义了User类,它是数据模型和下面的NotifierProvider类。在NotifierProvider类中,我有一个函数saveUser(),它根据数据是新的还是从数据库中检索到的来填充变量newUser。notifierProvider类属于User类型。
class Users {
String? userId;
String? fName;
String? lName;
String? address1;
String? address2;
String? city;
String? state;
String? zipCode;
String? cellPhone;
String? officePhone;
String? email;
String? company;
String? mls;
Users(
{this.userId,
this.fName,
this.lName,
this.address1,
this.address2,
this.city,
this.state,
this.zipCode,
this.cellPhone,
this.officePhone,
this.email,
this.company,
this.mls});
Users copyWith({
String? userId,
String? fName,
String? lName,
String? address1,
String? address2,
String? city,
String? state,
String? zipCode,
String? cellPhone,
String? officePhone,
String? email,
String? company,
String? mls,
}) {
return Users(
fName: fName ?? this.fName,
lName: lName ?? this.lName,
address1: address1 ?? this.address1,
address2: address2 ?? this.address2,
city: city ?? this.city,
state: state ?? this.state,
zipCode: zipCode ?? this.zipCode,
cellPhone: cellPhone ?? this.cellPhone,
officePhone: officePhone ?? this.officePhone,
email: email ?? this.email,
company: company ?? this.company,
mls: mls ?? this.mls,
);
}
}
NotifierProvider类的类型为Users,即上面的类。那么,既然我已经将类声明为User类,为什么我在UsersNotifier类中的所有数据元素上都得到“undefined”错误?它们不应该因为通告程序类的类型声明而被声明吗?
如果我将鼠标悬停在扩展通知程序的用户上,一个弹出窗口会显示我正在使用user_provider.dart文件中的“Class Users”,该文件与通知程序类所在的文件相同。
class UsersNotifier extends Notifier<Users> {
final firestoreService = FirestoreService();
final companyRef = FirebaseFirestore.instance.collection(('Company'));
final userRef = FirebaseFirestore.instance.collection(('users'));
// **************************************************
@override
Users build() {
return Users(
userId: '',
fName: '',
lName: '',
address1: '',
address2: '',
city: '',
state: '',
zipCode: '',
cellPhone: '',
officePhone: '',
email: '',
company: '',
mls: '',
);
}
// 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 updateEmail(String newEmail) {
state = state.copyWith(email: newEmail);
}
void updateCompany(String newCompany) {
state = state.copyWith(company: newCompany);
}
void updateMls(String newMls) {
state = state.copyWith(mls: newMls);
}
// **************************************************
saveUser(ref) async {
if (ref.watch(globalsNotifierProvider).newUser == true) {
final DocumentSnapshot currentCompanyProfile =
await companyRef.doc(ref.watch(globalsNotifierProvider).companyId).get();
var newUser = Users(
userId: ref.watch(globalsNotifierProvider).currentUserId,
fName: fName, <<< undefined name 'fName'
lName: lName, <<< undefined name 'fLame'
mls: mls, <<< undefined name 'mls'
mlsId: ref.watch(globalsNotifierProvider).mlsId,
address1: currentCompanyProfile.get('address1'),
address2: currentCompanyProfile.get('address2'),
city: currentCompanyProfile.get('city'),
state: currentCompanyProfile.get('state'),
zipcode: currentCompanyProfile.get('zipCode'),
cellPhone: currentCompanyProfile.get('cellPhone'),
officePhone: currentCompanyProfile.get('officePhone'),
companyId: ref.watch(globalsNotifierProvider).companyId;
company: company), <<< undefined name 'company'
firestoreService.saveUser(newUser, ref);
ref.read(globalsNotifierProvider.notifier).updatenewUser(false);
} else {
final DocumentSnapshot currentuserProfile =
await userRef.doc(ref.read(globalsNotifierProvider).currentuserId).get();
var newUser = Users(
userId: ref.read(globalsNotifierProvider).currentUid,
fName:
(fName != null && fName != "") ? fName : currentuserProfile.get('fName'),
lName:
(lName != null && lName != "") ? lName : currentuserProfile.get('lName'),
address1: (address1 != null && address1 != "")
? address1
: currentuserProfile.get('address1'),
address2: (address2 != null && address2 != "")
? address2
: currentuserProfile.get('address2'),
city: (city != null && city != "") ? city : currentuserProfile.get('city'),
state: (state != null && state != "") ? state : currentuserProfile.get('state'),
/*state: (globals.selectedState != null)
? globals.selectedState
: globals.currentuserState,*/
zipcode: (zipCode != null && zipCode != "")
? zipCode
: currentuserProfile.get('zipCode'),
cellPhone: (cellPhone != null && cellPhone != "")
? cellPhone
: currentuserProfile.get('cellPhone'),
officePhone: (officePhone != null && officePhone != "")
? officePhone
: currentuserProfile.get('officePhone'),
companyId: ref.read(globalsNotifierProvider).CompanyId,
company: (company != null && company != "")
? company
: currentuserProfile.get('Company'),
mlsId: ref.read(globalsNotifierProvider).mlsId,
mls: (mls != null && mls != "")
? mls
: currentuserProfile.get('mls'));
firestoreService.saveUser(newUser, ref);
}
}
deleteUser(String? userId) {
firestoreService.deleteUser(userId);
}
}
final usersNotifierProvider = NotifierProvider<UsersNotifier, Users>(() {
return UsersNotifier();
});
在NotifierProvider类中,我得到了很多未定义的变量,一些命名的参数说它们是未定义的。
在var newUser = User(code中,所有变量都是未定义的。我认为这可能与第一行有关,我从globalsNotifierProvider中分配了一个全局变量:
userId: ref.read(globalsNotifierProvider).currentUid,
“else”语句是我得到大多数错误的地方。每个变量都是未定义的。
我可以得到帮助为什么会发生这种情况?我知道这将是一件简单的事情,但我就是看不出来。
Thanks in advance
1条答案
按热度按时间pb3skfrl1#
类
UsersNotifier
没有扩展Users
,错误是相当准确的,* 在this
* 中没有x
。这是因为类不能扩展或实现泛型(T
)。通常ide会给你一个给予警告,告诉你不能做什么,但是当你使用动态和/或空变量时,ide不能确定你是在施魔法还是犯了错误。
这个例子不是一对一的
Notifier
是做什么,但它有相同的错误,你应该记住,你可能无法访问它,如果它是一个私有变量(_generic
)。如docs中所述,
state
是我示例中的generic
。如果你想了解更多关于它在做什么或如何做的细节,只需检查类的继承行。简而言之,使用
state.fName
等代替fName
。