Back to results for “Async await”
Flutter › REST API Integration in Flutter

How should you properly design your app's own networking layer to genuinely support parallel loading of several genuinely independent pieces of data needed for a single given screen, while still properly and gracefully handling the case where just one of those particular requests genuinely happens to fail?

Advanced

Answer

For a screen genuinely requiring several separate independent pieces of data, such as a dashboard needing to fetch a user's own profile, their recent orders, and their current notification count all together, using Future.wait to properly load all of those genuinely independent requests concurrently rather than sequentially one after another meaningfully improves your screen's own overall load time, and properly handling the specific case where just one of those particular several requests genuinely happens to fail, such as by using Future.wait with the eagerError parameter set to false or individually catching each request's own separate error, lets you still properly display whichever specific pieces of data genuinely did successfully load, rather than the entire screen's overall loading process failing completely just because one single genuinely non critical particular piece of data happened to fail.
final results = await Future.wait([
  fetchProfile().catchError((e) => null),
  fetchOrders().catchError((e) => <Order>[]),
  fetchNotificationCount().catchError((e) => 0),
]);
Real-world example A dashboard screen loads a user's profile, recent orders, and notification count all concurrently, properly catching any individual specific failure so a genuinely non critical failed notification count fetch does not prevent the rest of that dashboard's own successfully loaded data from still being properly displayed.

Common follow-ups: What genuinely different specific strategies exist for handling a partial failure like this beyond simply using individual catchError calls?;How do you actually properly and clearly indicate to the user within the interface itself that one specific particular piece of dashboard data genuinely failed to load?