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
.