[英]How do i use StateNotifier riverpod to track the changes of enum value
[英]How can I use riverpod's statenotifier for handling a form?
我正在嘗試使用 StateNotifier 來處理表單,但在查看 stateNotifierProvider 時,我得到了 object 我正在嘗試更新而不是提供程序。
這是我的實現:
final profileCreationFormNotifierProvider =
StateNotifierProvider((ref) => ProfileCreationFormNotifier());
class ProfileCreationFormNotifier extends StateNotifier<ProfileModel> {
ProfileCreationFormNotifier()
: super(
ProfileModel(
city: '',
country: '',
firstname: '',
lastname: '',
pictureUrl: '',
username: '',
gender: '',
),
);
void setFirstname(String firstname) {
state = state.copyWith(firstname: firstname);
}
void setLatstname(String lastname) {
state.copyWith(lastname: lastname);
}
void setUsername(String username) {
state.copyWith(username: username);
}
void setGender(String gender) {
state.copyWith(gender: gender);
}
}
然后在消費者小部件中,我嘗試調用方法來更新表單上的字段,如下所示
Consumer(
builder:
(BuildContext context, ScopedReader watch, Widget? child) {
return CustomTextField(
controller: usernameFieldController,
hintText: 'Username',
isEmail: false,
onSave: (value) {
context
.read(profileCreationFormNotifierProvider)
.setUsername(value);
},
onValidate: (value) => validator(value!, "Username"),
secured: false,
);
},
),
我收到以下錯誤:
Class 'ProfileModel' 沒有實例方法 'setUsername'。 接收方:“ProfileModel”實例嘗試調用:setUsername("qsqsqsq")
我在這里做錯了什么?
我正在使用 hook_riverpod 版本:“0.14.0+2” 謝謝你的幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.