Dart: optimizing simultaneous data fetching

Let’s say we have a class ProductService , which is a singleton, like this: class ProductService { static ProductService? _instance; static ProductService get instance => _instance!; Future<List<Product>> fetchProducts() async { // Perform the HTTP request and transform the result } } In various places, we can call the method fetchProducts as: ProductService.instance.fetchProducts().then((v) { ... }); The problem is that this method might be called from various places simultaneously, including in multiple FutureBuilders for example, which will be wasting bandwidth and unnecessarily loading the server, and potentially slowing the app down....

July 7, 2024

How to use Flutter’s FutureBuilder properly

When developing a Flutter app, there might be a need to display data that is not instantly available. This data needs to be prepared asynchronously to prevent freezing of the app. And while the data is being prepared, we want to display an indicator so the user knows that their data is being loaded. Fortunately, the Flutter framework has FutureBuilder widget that fits well for this use case. The official documentation does provide explanations on how to use the FutureBuilder, but I find that turning it into a set of practical rules makes it easier to follow especially to avoid the misusages....

June 20, 2024