【发布时间】:2021-03-22 22:54:07
【问题描述】:
为什么我们有时会使用 notifyListeners,以及为什么我们不使用 notifyListeners?如何在 changenotifier 中使用函数?
例如,在这段代码中,有时我们使用 notifyListeners,但有时我们没有使用 notifyListeners(在 login() 函数中)。为什么 ?我们什么时候使用 notifyListeners?
String get userEmail => _userEmail;
set userEmail(String value) {
_userEmail = value;
notifyListeners();
}
String get userPassword => _userPassword;
set userPassword(String value) {
_userPassword = value;
notifyListeners();
}
String get userName => _userName;
set userName(String value) {
_userName = value;
notifyListeners();
}
DateTime get dateOfBirth => _dateOfBirth;
set dateOfBirth(DateTime value) {
_dateOfBirth = value;
notifyListeners();
}
Future<bool> login() async {
try {
isLoading = true;
print(userEmail);
print(userPassword);
if (isLogin) {
await FirebaseAuth.instance.signInWithEmailAndPassword(
email: userEmail,
password: userPassword,
);
} else {
await FirebaseAuth.instance.createUserWithEmailAndPassword(
email: userEmail,
password: userPassword,
);
}
isLoading = false;
return true;
} catch (err) {
print(err);
isLoading = false;
return false;
}
}
}
除了有人可以回答我为什么我们在这段代码中使用 set 方法
bool get isLogin => _isLogin;
set isLogin(bool value) {
_isLogin = value;
notifyListeners();
}
【问题讨论】:
标签: flutter dart state-management flutter-change-notifier