I am building a flutter application in which I have 2 screens.I have API calls on both screens.When I navigate on 2nd screen and return back to 1st screen then build function of 1st Screen runs as expected and API is called again(In future of FutureBuilder) which I don't want. I only want to update or fetch the API when I slide from top to bottom on screen.But due to FutureBuilder in my !st screen's build function it is fetching the data again and again everytime. Any suggestion with e.g. code will be helpful.Thank you!
Asked
Active
Viewed 260 times
1 Answers
0
you can use StatefullWidget
and have a property in State
class like Future<String>
and assing it in initState
.
if you back from second screen your build function called but you have previous data.
...
Future<String> future;
initState(){
future = apiCall();
}
Widget build(){
return FutureBuilder(
future: future,
);
}

Hadi Norouzi
- 179
- 2
- 8
-
But FutureBuilder which is inside build function will keep calling the future then what's the solution to that.It will keep calling the API. – Gautam Goyal Mar 24 '21 at 16:21