Here’s my use case: On the home screen,
I’m making a few API calls. Additionally, I want to upload stored data, which could involve 5 or more API calls. I’m considering using a for loop to make these calls.
My concern is whether it will affect the app performance. If yes, then what is the best way to achieve the above use case using the HTTP package?
2
Answers
Of course it would affect your app, because each API call sends a package to server & server gives back a result package to your app… That round trip would take a little time, bases on your network connection and how strong is your server that API calls will be like QuickSilver or a turtle.
if you are making 5 or more API calls, using a for loop to make these calls will definitely affect the app performance. This is because the for loop will make each API call sequentially, which means that the next API call will not start until the previous one has finished. This can lead to a significant delay in the execution of your code.
The best way to achieve your use case using the HTTP package is to use the Future API. The Future API allows you to make asynchronous API calls, which means that you can make multiple API calls at the same time. This will significantly improve the performance of your app, as the API calls will not block each other.
You can consider to use Future.wait if you APIs can be call in parallel.
Definetly;
Using a for loop to make multiple API calls can have performance issues, especially if you make synchronous calls, as it can lead to increased response times and potential blocking of the user interface.
But You can use Asynchronous Calls as @Nguyen family answer->
Instead of making synchronous API calls in a for loop, use asynchronous calls to make concurrent requests. This way, the app won’t wait for each API call to complete before proceeding to the next one, resulting in faster data retrieval and improved responsiveness.
And also try using Batch API calls.-> Try to batch the API calls into groups rather than making individual calls for each data upload.
And also if you have control over the server, ensure that the API endpoints are optimized for handling multiple requests efficiently. This can include implementing caching mechanisms, using indexes in databases, and optimizing database queries.
I think using one or more these strageies you can achieve a better app performance while making multiple API calls and uploading stored data.
*note
And also you can use something like loading screen like games till the complete all API Calls.