0

I'm trying to run the application but the setState error is showing at MaterialApp where I have not used 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 i.e the MaterialApp Function.

>   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
cactus4
  • 128
  • 9

1 Answers1

0

Please provide the code where have you called the setState((){}) function.

But given the error I can tell you're trying to call setState in the init function.

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.