I have a variable late Future<QuestionModel> question;
and a function that fetches QuestionModel
fetchQuestion(){
// Question model is initialised here
}
It is being used in build to display a UI
FutureBuilder(
future: question,
builder: (context, snapshot) {
if (snapshot.data == null) {
return LoadingWidget(
height: 200,
width: 200,
);
}
if (snapshot.hasError) {
return Text("Something went wrong");
}
QuestionModel question = snapshot.data as QuestionModel;
return Container(),
);
}
),
Which is working fine.
In the UI itself I have a button which sends data to backend and fetches new QuestionModel from Firestore. Which is also working fine.
Requirement: To Show loading screen after clicking a button and avoid reclicking till the new QuestionModel is fetched but I don’t want to create a separate isLoading
variable for that.
I just want to reset the questionModel which was initialised earlier as questionModel = null
and show loading screen by making use of futurebuilder itself as I already have LoadingWidget attached to my FutureBuilder.
But that is not possible as Future can’t be null with below error
A value of type 'Null' can't be assigned to a variable of type 'Future<QuestionModel>'.
EDIT 2:
In other words I want to uninitialize the future to trigger my Future builder to show loading Widget.
2
Answers
try the demo here to understand more
https://dartpad.dev/?id=3dadbc210baaf70d405b75e5389c7ba8
you can use
setState
to rebuild theFutureBuilder
.But there are behavior for
FutureBuilder
will not execute thefuture
method when you define it aslocal state variable
solution:
helper.dart
then in your
FutureBuilder
widget, use condition forConnectionState.waiting
,and every time you call
setState
theapiCall
will executedps: if you use
snapshot.hasData
, it will betrue
even theapiCall
not finish yet. better to listern the connection state.Change declaration to this :
late Future<QuestionModel>? question;
Change your code to this :