0

I am using FutureBuilder inside my Build method and the FutureBuilder keeps firing up again and again. I only want to call FutureBuilder once until my future is loaded and then call another function as soon as it is done. This is my Build function -

@override
Widget build(BuildContext context) {
    return FutureBuilder(
      future: parsings(),
      builder:
          (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
        switch (snapshot.connectionState) {
          case ConnectionState.waiting:
            return LoadingState(context);
          default:
            if (snapshot.hasError)
              return new Text(
                  'Error in snapshot: \n${snapshot.error}');
            else {
              return func1();
            }
        }
      },
    );
  }

As i said, my Build function keeps on building again and again.

Before this, i was using my future inside initState but because of that, my actual page that i wanted to return gave null values all over until the API was called. I dont want to see null values at all so i wanted to use FutureBuilder and display LoadingState() until the API was called and then show my page which has all the called values and doesnt show null values. Any help would be appreciated.

EDIT: The detailed issue i am facing is as the build function is being called again and again, i am seeing my LoadingState() again and again, that is, LoadingState() is appearing and then func1() is appearing then again, LoadingState(), then func1() and so on. this process does not stop at all. Also, i do not want to use parsings() in initState because on doing so, before the API is called, the func1() shows null values in my data and as soon as it is called, the build function changes its values to the values called from API. I don't want to see it like that which is why i wanted to use FutureBuilder.

  • Looks like you want to read https://stackoverflow.com/questions/52249578/how-to-deal-with-unwanted-widget-build – Rémi Rousselet Jun 18 '20 at 08:54
  • @RémiRousselet I saw that but my widget seems to be changing all the time so that solution didnt work for me, instead i put parsings() in a Future variable and awaited for it and in the future property of FutureBuilder put that Future variable, it seemed to do the job, idk how. – Kushagra Srivastava Jul 07 '20 at 15:22
  • @Uni didnt work :( – Kushagra Srivastava Jul 07 '20 at 15:22
  • @KushagraSrivastava Here is the solution to that problem: https://stackoverflow.com/questions/57793479/flutter-futurebuilder-gets-constantly-called – CoderUni Jul 08 '20 at 02:11

0 Answers0