Why is FutureBuilder called multiple times? Why is FutureBuilder called multiple times? dart dart

Why is FutureBuilder called multiple times?


Anything that's within a widget's build() method can trigger anytime — this is, you shouldn't rely on anything critical that could have impact on it. For example, opening a keyboard or closing, is enough to trigger multiple calls to build() on a widget.

Your FutureBuilder is just a widget on your tree, and because of that, anytime it rebuilds (for example, whenever you type on the TextField), it will call your getDataArray() because it doesn't actually really knows if that has already been called or not.

If you just want it to be called once, you can assign it to a state member and assign it to your FutureBuilder. That will be preserved as long as that state exists on the widget tree.

You can for example assign it on initState() if you want to call it only per state lifecycle or at didChangeDependencies() for example, if you want to refresh that call anytime a dependency changes.

 class Pagina3State extends State<Pagina3> {   Future myFuture;  @override  void initState() {     myFuture = getDataArray();  }  @override   Widget build(BuildContext context) {       child: FutureBuilder(              future: myFuture,      )   }}


You can create a variable which return futureBuilder, so when you call setState only widget go rebuild but variable don't, so you can solve your issue.

 Widget futureWidget;  @override  void initState() {    futureWidget = FutureBuilder(      future: callme(),      builder: (_, sanpshot) {        print("future method call");        if (sanpshot.hasData) {          return Text(sanpshot.data.toString());        }        return CircularProgressIndicator();      },    );    super.initState();  }  callme() async {    await Future.delayed(Duration(seconds: 1));    return "success";  }  @override  Widget build(BuildContext context) {    print("build method call");    return Scaffold(      body: Column(        mainAxisAlignment: MainAxisAlignment.center,        children: [          RaisedButton(            child: Text("press"),            onPressed: () {              setState(() {});            },          ),          futureWidget,        ],      ),    );  }


That's because you are using setstate in your textfield action and it rebuild everything. I faced with similar problem with one of my apps and used a bool to avoid it, I named it firstRun, if it is true I called my future function in future builder if it's not I made it null like below ode sample. But I am not sure that is best way to do it, also you need to make this bool false in somewhere (in initstate perhaps);

bool firstRun = true; @override    Widget build(BuildContext context) {...                            ...                             onChanged: (valor) {                        firstRun = false;                        setState(() {                            _input = valor;                        });                        },                      .....                     FutureBuilder(                            future: firtRun ? getDataArray() : null,                            builder: (...