skip to Main Content

I am using GetX.

When I go from EventScreen to HomeScreen -> Everything works…then going back to EventScreen and back to HomeScreen(using Drawer menu from left)…the second time I go back to the HomeScreen this error occurs:

[GETX] Instance "EventsScreenController" has been initialized
[GETX] GOING TO ROUTE /HomeScreen
[GETX] Instance "HomeScreenController" has been created
[GETX] Instance "HomeScreenController" has been initialized
[GETX] GOING TO ROUTE /EventsScreen
[GETX] GOING TO ROUTE /HomeScreen
[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: setState() or markNeedsBuild() called during build.
This Obx 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:
  Obx
The widget which was currently being built when the offending call was made was:
  Builder
#0      Element.markNeedsBuild.<anonymous closure> (package:flutter/src/widgets/framework.dart:4634:9)
#1      Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:4646:6)
#2      State.setState (package:flutter/src/widgets/framework.dart:1153:15)
#3      _ObxStat<…>

Why this is occurs the second time I go to HomeScreen?

Some code in HomeScreen:

              child: Obx(
                () {
                  return controller.isLoading.value
                      ? const Center(child: CircularProgressIndicator())
                      : controller.isError.value
                          ? ErrorCard(
                              controller: controller,
                              retryFunction: () => controller.getEvents(),
                            )
                          : controller.events.isEmpty
                              ? const Text(
                                  "There are no events available for the defined filter") //TODO: Show ErrorCard and set retryFuntion: openFilter of events (popup)
                              : ListView.builder(
.......)})

5

Answers


  1. try this

        child: Obx(
                () {
          if(controller.isLoading.value == true){
            return const Center(child: CircularProgressIndicator());
          }      
           return controller.isError.value
                          ? ErrorCard(
                              controller: controller,
                              retryFunction: () => controller.getEvents(),
                            )
                          : controller.events.isEmpty
                              ? const Text(
                                  "There are no events available for the defined filter") //TODO: Show ErrorCard and set retryFuntion: openFilter of events (popup)
                              : ListView.builder(
    ....)})
    
    Login or Signup to reply.
  2. From the code given, I couldn’t find any error that could cause the said error.

    The error is telling you that something (probably controller) has changes (property value has changed for example) and it needs rebuild while the build is building it.

    I’m not sure where the source of the error without the full code. However, you may try to move the initialization of the controller out of the build method, if you haven’t had.

    Hope this help.

    final YourController controller = Get.put(YourController());
    
    @override
    Widget build(context){
    ...
    }
    
    Login or Signup to reply.
  3. If you are using a stateful widget, You are updating your value on the initstate method then please use this.

    initState(){
     super.initState();
      WidgetsBinding.instance.addPostFrameCallback((_) {
      // executes after build
      });
    }
    

    Notes:- You are updating the value before build widget.

    Login or Signup to reply.
  4. As the log says setState() or markNeedsBuild() called during build this is because you are using StatefulWidget or may be initialize something in initState()

    The solution is to make it StatelessWidget and if you want to initialize something when state is created use onInit() methood of GetxController.

    happy coding 🙂

    Login or Signup to reply.
  5. you may calling controller without initState , you may call to load data in initState

      initState(){
         super.initState();
          Future.delayed(Duration.zero, () {
        // write Code
            });
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search