title: How to set a timer to update API data periodically in Flutter
body: <p>I'm getting data from an API and load it to a listview.  My listview is showing the result of the query without any problem. I want to update my listview by periodically getting data from API. What is the best way to do this?</p>
  
  <p>This is how I'm calling API method in initState of Home Screen.</p>
  
  <pre><code>@override
    void initState() {
      // TODO: implement initState
      super.initState();
      _fixtureData = getFixture();
    }
  
    Future&lt;List&lt;Fixtures&gt;&gt; getFixture() async {
      fixtureList = await FootballApi.getFixtureData();
      return fixtureList;
    }
  </code></pre>
  
  <p>This is the code which I render ListView by FutureBuilder in Home Screen.</p>
  
  <pre><code>              FutureBuilder&lt;List&lt;Fixtures&gt;&gt;(
                    future: _fixtureData,
                    builder: (context, snapshot) {
                      if (snapshot.hasData) {
                        fixtureList = snapshot.data;
  
                        return AppListView(
                          matchList: fixtureList,
                          //callback function brings the matchCounter value from ListView class
                          onChange: (value) {
                            setState(() {
                              matchCounter = value;
                            });
                          },
                          finalBetList: (value) {
                            setState(() {
                              betList = value;
                            });
                          },
                        );
                      }
                      return Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: &lt;Widget&gt;[
                          SizedBox(
                            child: CircularProgressIndicator(),
                            width: 60,
                            height: 60,
                          ),
                          const Padding(
                            padding: EdgeInsets.only(top: 16),
                            child: Text(
                              'Awaiting result...',
                              style: TextStyle(color: Colors.white),
                            ),
                          )
                        ],
                      );
                    },
                  ),
  </code></pre>
  
  <p>And this snippet is the API method that I call.</p>
  
  <pre><code>static Future&lt;List&lt;Fixtures&gt;&gt; getFixtureData() async {
      Map&lt;String, String&gt; queryParameters = {
        'league': '79',
        'next': '5',
      };
  
  
      http.Response response = await http.get(
        getUrl('fixtures', queryParameters),
        headers: requestHeaders,
      );
  
      if (response.statusCode == 200) {
        String data = response.body;
        List&lt;dynamic&gt; result = jsonDecode(data)['response'];
  
        for (int i = 0; i &lt; result.length; i++) {
          Fixtures fixture = Fixtures();
  
          fixture.leagueID = jsonDecode(data)['response'][i]['league']['id'];
          fixture.country = jsonDecode(data)['response'][i]['league']['country'];
          fixture.leagueName = jsonDecode(data)['response'][i]['league']['name'];
          fixture.fixtureID = jsonDecode(data)['response'][i]['fixture']['id'];
  
          //get Odds to match with fixtures by fixtureID
          await getOddsData(fixture.fixtureID);
  
          fixture.dateTime =
              DateTime.parse(jsonDecode(data)['response'][i]['fixture']['date']);
          fixture.homeTeam =
              jsonDecode(data)['response'][i]['teams']['home']['name'];
          fixture.awayTeam =
              jsonDecode(data)['response'][i]['teams']['away']['name'];
          fixture.status =
              jsonDecode(data)['response'][i]['fixture']['status']['long'];
          fixture.homeGoals = jsonDecode(data)['response'][i]['goals']['home'];
          fixture.awayGoals = jsonDecode(data)['response'][i]['goals']['away'];
          fixture.htScoreHome =
              jsonDecode(data)['response'][i]['score']['halftime']['home'];
          fixture.htScoreAway =
              jsonDecode(data)['response'][i]['score']['halftime']['away'];
          fixture.ftScoreHome =
              jsonDecode(data)['response'][i]['score']['fulltime']['home'];
          fixture.ftScoreAway =
              jsonDecode(data)['response'][i]['score']['fulltime']['away'];
  
          if (oddsList.length &gt; 0) {
            for (int j = 0; j &lt; oddsList.length; j++) {
              if (oddsList[j].fixtureID == fixture.fixtureID) {
                fixture.homeOdds = oddsList[j].homeOdds;
                fixture.drawOdds = oddsList[j].drawOdds;
                fixture.awayOdds = oddsList[j].awayOdds;
                fixture.bookmakerName = oddsList[j].bookmakerName;
                FootballApi.fixtureList.add(
                    fixture);
              }
            }
          }
        }
      } else {
        print('statusCode: ' + response.statusCode.toString());
      }
  
      return FootballApi.fixtureList;
    }
  </code></pre>