[英](Getting the error while building) setState() or markNeedsBuild() called during build.The relevant error-causing widget was MaterialApp
I'm trying to run the application but the setState error
is showing at MaterialApp
where I have not used setState
.我正在尝试运行该应用程序,但在
MaterialApp
中显示了setState error
,但我没有使用setState
。 I tried this solution but it didn't work for me.我尝试了这个解决方案,但它对我不起作用。 The Code is a part of the
main.dart
file, the application is getting started but when I'm trying to navigate to the other page it's showing me the error at main.dart
ie the MaterialApp
Function.该代码是
main.dart
文件的一部分,应用程序正在启动,但是当我尝试导航到其他页面时,它向我显示main.dart
的错误,即MaterialApp
Z86408593C2F85263
> return MaterialApp( //error here
title: 'KoylaMandi',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.grey,
appBarTheme: AppBarTheme(
iconTheme: IconThemeData(
color: Colors.white,
size: 34.0,
),
),
visualDensity: VisualDensity.adaptivePlatformDensity,
textTheme: TextTheme(
bodyText1: TextStyle(
color: kPrimaryFontColor,
),
bodyText2: TextStyle(
color: kPrimaryFontColor,
),
),
),
initialRoute: '/',
routes: {
'/': (context) => SplashScreen(),
'/phonesignup':(context) => MyLoginPage(),
'/recent': (context) => PlacedordersWidget(),
// '/signInbyOtp': (context) => SignInbyOtp(),
'otp': (context) => Otp(),
'/signIn': (context) => SignIn(),
'/home': (context) => Home(),
'/search': (context) => Search(),
'/profile': (context) => ProfilePage(),
'/resetPass': (context) => PasswordResetPage(),
'/signUp': (context) => SignUp(),
'/myCart': (context) => MyCartPage(),
'/about': (context) => AboutPage(),
'/contact': (context) => ContactPage(),
'/orders': (context) => MyOrderPage(),
'/adminsplashScreen': (context) => AdminSplashScreen(),
'/adminsignIn': (context) => AdminSignIn(),
'/adminhome': (context) => AdminHome(),
'/adminprimaryAd': (context) => PrimaryAdPage(),
'/adminsecondaryAd': (context) => SecondaryAdPage(),
'/admincategory': (context) => CategoriesPage(),
'/adminproducts': (context) => ProductPage(),
'/adminorders': (context) => OrdersPage(),
'/admininquiry': (context) => InquiryPage(),
'/adminqueries': (context) => ProductQueries(),
'/adminaddProduct': (context) => AddProduct(),
'/adminaddProperty': (context) => AddProperty(),
},
);
====================================ERROR==========================================
The following assertion was thrown building Builder:
setState() or markNeedsBuild() called during build.
This _ModalScope<dynamic> widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was: _ModalScope<dynamic>-[LabeledGlobalKey<_ModalScopeState<dynamic>>#48aee]
state: _ModalScopeState<dynamic>#e2ff2
The widget which was currently being built when the offending call was made was: Builder
The relevant error-causing widget was
MaterialApp
lib\main.dart:48
When the exception was thrown, this was the stack
#0 Element.markNeedsBuild.<anonymous closure>
package:flutter/…/widgets/framework.dart:4292
#1 Element.markNeedsBuild
package:flutter/…/widgets/framework.dart:4307
#2 State.setState
package:flutter/…/widgets/framework.dart:1264
#3 _ModalScopeState._routeSetState
package:flutter/…/widgets/routes.dart:769
#4 ModalRoute.setState
package:flutter/…/widgets/routes.dart:895
Please provide the code where have you called the setState((){}) function.请提供您调用setState((){}) function 的代码。
But given the error I can tell you're trying to call setState in the init function.但是考虑到错误,我可以告诉您正在尝试在init function 中调用setState 。
You can easily eliminate this error by checking whether the widget is mounted or not.您可以通过检查小部件是否已安装来轻松消除此错误。
if(!mounted) return;
setState(() {});
This will call setState if the widget tree is not mounted.如果未安装小部件树,这将调用setState 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.