简体   繁体   English

(在构建过程中出现错误)在构建过程中调用了 setState() 或 markNeedsBuild()。相关的导致错误的小部件是 MaterialApp

[英](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.

相关问题 相关的导致错误的小部件是 Scaffold - The relevant error-causing widget was Scaffold flutter 错误,相关的导致错误的小部件是 Scaffold - flutter error , The relevant error-causing widget was Scaffold flutter 应用程序中的“setState() 或 MarkNeedsBuild() 调用期间”错误 - “setState() or MarkNeedsBuild() called during” error in flutter app 如何将函数从子类传递到父类,我收到此错误:在构建期间调用了 setState() 或 markNeedsBuild() - How to pass function from a child class to parent class, I am getting this error: setState() or markNeedsBuild() called during build “在构建过程中调用了 setState() 或 markNeedsBuild()”如何解决这个问题? - "setState() or markNeedsBuild() called during build" how to resolve this? 在构建 TextField 期间调用 setState() 或 markNeedsBuild() - setState() or markNeedsBuild() called during build TextField 相关的导致错误的小部件是 Container lib\screens\home_screen.dart:18 - The relevant error-causing widget was Container lib\screens\home_screen.dart:18 为什么我不能将 Listview 添加到 Flutter? (相关的导致错误的小部件是 Column) - Why cant i add Listview to Flutter ? (The relevant error-causing widget was Column) 无限滚动列表视图 - 在构建期间调用的setState()或markNeedsBuild - Infinite Scroll listview - setState() or markNeedsBuild called during build setState() 或 markNeedsBuild() 在构建期间调用。 在导航推送中 - setState() or markNeedsBuild() called during build. in Navigator Push
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM