setState() 或 markNeedsBuild() 在构建期间调用。 Flutter 提供程序包

setState() or markNeedsBuild() called during build. Flutter Provider Package

homepage.dart

@override
void initState() {
super.initState();    
_userProfile = Provider.of<UserDataProvider>(context,listen: false).userData;
if (_userProfile['isPaid'] == false) {
  Navigator.of(context).pushNamed(PaymentScreen.routeName);
 }
}

错误

setState() or markNeedsBuild() called during build.

这个错误困扰着我。请帮我解决这个问题。 谢谢

您可以使用如下代码:

@override
void initState() {
super.initState();    
Future.delayed(Duration.zero, (){
  _userProfile = Provider.of<UserDataProvider>(context,listen: false).userData;
  if (_userProfile['isPaid'] == false) {
    Navigator.of(context).pushNamed(PaymentScreen.routeName);
  }
 });
}