본문 바로가기
Flutter

[Flutter] flutter multiple api calls ; 여러개의 response 처리하기

by hymndaniel 2022. 4. 27.
  Future _fetchData() async {
    setState(() {
      _showLoading = true;
    });

    final responses = await Future.wait([
      httpHelper.fetchWorkData(widget.userId, widget.selectedDate, "day"),
      httpHelper.fetchCutivationStepData(widget.selectedDate),
      httpHelper.fetchFieldListData(widget.userId),
      httpHelper.fetchWorkListData(),
      httpHelper.fetchPestListData(),
      httpHelper.fetchDisorDerListData(),
    ]);

    setState(() {
      todayDataList = responses[0];
      cultivationStepData = responses[1];
      fieldListData = responses[2];
      workListData = responses[3];
      pestListData = responses[4];
      disorderListData = responses[5];
    });

    setState(() {
      _showLoading = false;
    });
    print(fieldListData);
  }

여러개의 요청을 리스트에 받은 후 객체에 요청 결과를 각 각 할당하면 끝.

 

References

https://www.youtube.com/watch?v=rhw3y9iAak8 

 

https://stackoverflow.com/questions/59114243/flutter-best-way-to-request-multiple-apis-simultaneously

 

Flutter - Best way to request Multiple APIs simultaneously

I have two URLs, and I am using the fetchData() function to parse the json. Future<Iterable> fetchData() async { var response = await http.get(firstUrl); var listOne = List<Article>();...

stackoverflow.com

 

728x90