I am working on a project using Flutter with scoped_model
as state manager.
But there is one point that I'm not sure to understand.
I'm actually calling my API at every single build which looks weird to me.
What is the good practice to make API calls using state management as scoped_model
?
Here is my actual widget's code :
class RunsListPage extends StatelessWidget {
final ScopedRuns scopedRuns = ScopedRuns();
@override
Widget build(BuildContext context) {
this.scopedRuns.getRuns();
return ScopedModel<ScopedRuns>(
model: this.scopedRuns,
child: ScopedModelDescendant<ScopedRuns>(
builder: (context, child, scopedRuns) {
return Column(
children: <Widget>[
Expanded(
child: Stack(
children: <Widget>[
LoadingIndicatorWidget(
visible: scopedRuns.isLoading,
),
RefreshIndicator(
color: Theme.of(context).primaryColor,
onRefresh: () async => this.scopedRuns.getRuns(),
child: RunsList(
runsList: scopedRuns.runs ?? [],
visible: !scopedRuns.isLoading),
),
],
),
)
],
);
},
));
}}
and here is my getRuns()
method from my ScopedRuns
model :
getRuns() async {
_isLoading = true;
notifyListeners();
_runs = await APIService.getRuns();
_isLoading = false;
notifyListeners();
}