(在构建时出现错误)在构建期间调用 setState() 或 markNeedsBuild()。相关的导致错误的小部件是 MaterialApp

Posted

技术标签:

【中文标题】(在构建时出现错误)在构建期间调用 setState() 或 markNeedsBuild()。相关的导致错误的小部件是 MaterialApp【英文标题】:(Getting the error while building) setState() or markNeedsBuild() called during build.The relevant error-causing widget was MaterialApp 【发布时间】:2022-01-03 09:14:04 【问题描述】:

我正在尝试运行该应用程序,但setState error 显示在MaterialApp 上,而我没有使用setState。我尝试了this 解决方案,但它对我不起作用。该代码是main.dart 文件的一部分,应用程序正在启动,但是当我尝试导航到另一个页面时,它向我显示main.dart 的错误,即MaterialApp 函数。

>   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

【问题讨论】:

【参考方案1】:

请提供您调用 setState(()) 函数的代码。

但鉴于错误,我可以告诉您正在尝试在 init 函数中调用 setState

您可以通过检查小部件是否已安装来轻松消除此错误。

if(!mounted) return;
setState(() );

如果小部件树没有挂载,这将调用 setState

【讨论】:

成功了,非常感谢! 很好,请您将此标记为已回答? 是的,非常感谢您

以上是关于(在构建时出现错误)在构建期间调用 setState() 或 markNeedsBuild()。相关的导致错误的小部件是 MaterialApp的主要内容,如果未能解决你的问题,请参考以下文章

Flutter:在构建错误期间调用了 setState() 或 markNeedsBuild()

Flutter 错误 - 在构建期间调用了 setState() 或 markNeedsBuild()

Flutter:在构建期间调用 setState() 或 markNeedsBuild()

在构建期间调用 setState() 或 markNeedsBuild() - Flutter

Flutter - 在构建期间调用 setState() 或 markNeedsBuild()

如何修复 Flutter 构建问题期间调用的 setState