Overcoming Obstacles: How to Implement a Retry Strategy in Dart to Enhance User Experience

Carlos Daniel
3 min readMay 30, 2023
Photo by 愚木混株 cdd20 on Unsplash

Spanish version available.

In the fascinating world of mobile app development, we come across situations where things don’t always go as planned. Sometimes, we face temporary or unpredictable obstacles that can lead to failures in our operations, such as internet connectivity issues or server delays, just to name a few. From there, we ask ourselves: How can we deal with these challenges and ensure that our applications are robust and reliable? That’s where the “retry” strategy comes into play.

What is a “retry” strategy and why is it important?

A “retry” strategy is a technique that allows us to handle temporary failures in the internal operations of our applications, giving them the ability to recover automatically. Instead of giving up and considering an operation as immediately failed, the “retry” strategy allows us to try again as many times as necessary, after a specified period of time. This strategy offers several significant benefits:

  1. Robustness and Resilience: By implementing a retry strategy, our application becomes more robust and capable of handling temporary failures. Instead of collapsing in the face of a momentary difficulty, such as displaying an immediate error message, the application will retry after a certain period of time (e.g., 1 second or 200 milliseconds), increasing the chances of success. This gives us greater confidence in our application and allows us to effectively handle transient failures.
  2. Improved User Experience: Imagine the frustration that a user may experience when an operation fails on the first attempt. Retry allows us to provide a better user experience by automatically attempting retries without requiring manual intervention. This is particularly valuable in situations where the failure is temporary and the operation is likely to succeed in subsequent attempts. By avoiding unnecessary frustrations, we enhance user satisfaction and foster trust in our application.
  3. Automatic Recovery from Transient Errors: By retrying an operation multiple times, we have the opportunity to overcome temporary errors that may be resolved automatically in a subsequent attempt. This prevents users from having to take additional actions or seek alternatives, simplifying their workflow and improving efficiency. With the retry strategy, we can automate the recovery from transient errors and provide a smoother experience for users.
  4. Handling Overloads or Latencies: In situations of high load or unexpected latencies, a retry strategy can help us balance the load and better handle temporary situations where our systems are overloaded. By retrying after a brief interval, we allow the systems to recover or the latencies to decrease, contributing to greater stability and overall performance.
  5. How to Implement It?
    (Disclaimer: This is not the only way to implement it, but it is an approach that has worked well for us)

First, we define a custom type called FunctionOnRetry. This type is passed as a parameter to our retry function, which takes the initial function to be executed (doIt), the maximum number of retries (maxRetries) until it reaches a success, and a delay in milliseconds (delay) indicating how often the doIt function should be retried. Repetitively, we make the call until we achieve the success of the operation or encounter an exception flow.

And a way to use it, below:

We realize then that its usage is simple, it’s just a matter of being clear about what we want to do and what fallback action we want to take if needed. We configure the number of retries and the frequency of their execution.

In conclusion, implementing this strategy can make a difference in the reliability of our applications. By automatically handling temporary errors that may occur and allowing operations to succeed in subsequent attempts, we improve the user experience and the resilience of the final app.

Final acknowledgments to Daniel Gómez and Julián Sotelo, who are also the intellectual and material authors of this implementation.

--

--

Carlos Daniel

Android & Flutter Developer. GDE for Android & Mobile Engineer.