0

I am displaying a popup survey in my app so after completing the survey and clicking done, the popup appears again. I do not want that to open.

I get this error setState() or markNeedsBuild() called during build which makes the widget show time and time again without completing.

I tried different solution but I cannot just get to the solution. Can I please get assistance where I am getting it wrong.

My code of my method:

  void showGenexPopup() {
    final GenexViewModel genexViewModel =
        Provider.of<GenexViewModel>(context, listen: false);

    if (!genexViewModel.videoTrigger) {
      genexViewModel.showSurvey(
        context: context,
        pageId: GenexPageId.SearchPage,
        duration: const Duration(seconds: 5),
      );
    }
  }

Where I am calling the method:

  Widget buildSearchResults(GlobalSearchViewModel globalSearchViewModel) {
    if (globalSearchViewModel.loadingStatus == LoadingStatus.busy) {
      return getLoader();
    } else {
      switch (categorizedResults.length) {
        case 0:
          {
            return buildNoDataFound();
          }
        case 1:
          {
            return buildList(categorizedResults[0].categorizedResults);
          }
        default:
          {
            for (final CategorizedResults list in categorizedResults) {
              if (list.categorizedResults.isNotEmpty) {
                dynamicTabHeaders.add(list.categoryTitle);
                if (list.categoryTitle == 'News' ||
                    list.categoryTitle == 'Videos') {
                  dynamicContent.add(
                    Scaffold(
                      body: ArticleVideoListViewWidget(list.categorizedResults),
                    ),
                  );
                } else if (list.categoryTitle == 'Teams') {
                  for (final CategorizedTeams team in categorizedTeams) {
                    if (team.categorizedTeams.isNotEmpty) {
                      dynamicTeamTabHeaders.add(team.categoryTitle);
                      dynamicTeamContent.add(
                        Scaffold(
                          body: PlainListViewWidget(
                              searchValues: team.categorizedTeams),
                        ),
                      );
                    }
                  }
                  dynamicContent.add(
                    Scaffold(
                      body: PillTabBar(
                        tabHeadings: dynamicTeamTabHeaders,
                        tabBodies: dynamicTeamContent,
                        initialIndex: 0,
                        scrollViewScroll: false,
                      ),
                    ),
                  );
                } else {
                  dynamicContent.add(
                    Scaffold(
                      body: PlainListViewWidget(
                          searchValues: list.categorizedResults),
                    ),
                  );
                }
              }
            }

            showGenexPopup();
            return sliverController.singleRowSearchTabSliverWithoutHeader(
              dynamicTabHeaders,
              dynamicContent,
              0,
              screenWidth(context),
              dynamicTabHeaders.length > 3,
              tabCallBack: this,
            );
          }
      }
    }
  }

Help will be appreciated.

SaDev
  • 145
  • 1
  • 11

1 Answers1

0

This error happens when you try to setState or anything that sets this widget as dirty see the widget lifecycle for a better understanding before the build function finishes.

on your situation you are trying to show a dialog that as you can see from the Error it marks this widget as need build

consider using WidgetsBinding.instance.addPostFrameCallback to show the dialog after the first frame has been called.

Solution

 void showGenexPopup() {
WidgetsBinding.instance.addPostFrameCallback((_) {

   final GenexViewModel genexViewModel =
       Provider.of<GenexViewModel>(context, listen: false);

   if (!genexViewModel.videoTrigger) {
     genexViewModel.showSurvey(
       context: context,
       pageId: GenexPageId.SearchPage,
       duration: const Duration(seconds: 5),
     );
   }
});