当用户登录时,我处于当前状态,我从提供程序调用该方法,并根据状态渲染新状态。发生错误时,我想显示带有错误消息的小吃店按钮。
但是我遇到了以下错误:
错误
The following assertion was thrown building Consumer<AuthProvider>(dirty, dependencies: [_InheritedProviderScope<AuthProvider>]):
setState() or markNeedsBuild() called during build.
提供商
void signIn(String email, String password) async {
_logLoading(_loginStatus);
try {
print("Loggin with email $email and password $password");
_user = await _authService.signInWithEmailAndPassword(email, password);
_logSuccess(_loginStatus, 'You have logged successfully');
} catch (e) {
_logError(_loginStatus, e.toString());
}
}
void _logLoading(StatusHolder statusHolder) {
statusHolder.status = Status.Loading;
notifyListeners();
}
void _logSuccess(StatusHolder statusHolder, String message) {
statusHolder.status = Status.Success;
statusHolder.successMessage = message;
notifyListeners();
}
void _logError(StatusHolder statusHolder, String message) {
statusHolder.status = Status.Failure;
statusHolder.errorMessage = message;
notifyListeners();
}
小工具
Consumer<AuthProvider>(
builder: (context, model, child) {
switch (model.loginStatus.status) {
case Status.Loading:
return Center(
child: CircularProgressIndicator(),
);
case Status.Failure:
Scaffold.of(context).showSnackBar(SnackBar(
content: Text(model.loginStatus.errorMessage),
));
return _idlePart(context);
case Status.Success:
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => HomeScreen(),
));
return Container();
case Status.Idle:
default:
return _idlePart(context);
}
},
),