Continuous API call to fetch live data

Viewed 26

I am trying to make continuous api calls, every 250 ms to a sports api to fetch the latest scores in cricket. I am using Getx pattern along with it's state management to implement the same in flutter. Could you please specify me a way with which I can implement the same. My version of code is attached along.


Controller code is as follows

  final liveData = {}.obs;

  liveApiContinuousCall() async {
    liveData.value = {};
    Timer.periodic(const Duration(seconds: 1), (timer) async {
      http.Response liveResponse = await http.post(
        Uri.parse('${ApiConfig.baseUrl}${ApiConfig.liveMatchByMatchIdUrl}'),
        body: {'match_id': matchIdController.matchID.value},
      );

      var decodedData = jsonDecode(liveResponse.body);

      liveData.value = decodedData['data'];
    });
  }



The live data variable is then being used by me in view to render the UI.

0 Answers
Related