← Back to posts Cover image for Beyond FutureBuilder & StreamBuilder: Clean Asynchronous UI in Flutter

Beyond FutureBuilder & StreamBuilder: Clean Asynchronous UI in Flutter

· 5 min read
Weekly Digest

The Flutter news you actually need

No spam, ever. Unsubscribe in one click.

Chris
By Chris

When you’re building your first Flutter app, FutureBuilder and StreamBuilder feel like magic. Need to show data from an API? Wrap your widget in a FutureBuilder. Want real-time updates? StreamBuilder to the rescue. They work directly in your build() method, and everything just… works.

But as your app grows, that magic can turn into a maintenance nightmare. Let’s talk about why, and more importantly, what to do instead.

The Hidden Costs of Convenience

The core issue isn’t that FutureBuilder and StreamBuilder are inherently bad—they’re useful widgets. The problem emerges when we use them as our primary state management strategy in build() methods.

Here’s a typical pattern you’ve probably written:

class ProductPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder<Product>(
      future: fetchProductFromApi(), // This gets called on EVERY build
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return CircularProgressIndicator();
        }
        if (snapshot.hasError) {
          return Text('Error: ${snapshot.error}');
        }
        return ProductDetails(product: snapshot.data!);
      },
    );
  }
  
  Future<Product> fetchProductFromApi() async {
    // API call here
  }
}

This seems fine, but there are several problems lurking:

  1. Uncontrolled Re-fetching: Every time build() is called (which happens frequently in Flutter), your future is recreated. Without careful caching, you might be hitting your API dozens of times unnecessarily.

  2. Business Logic in UI: Your widget now knows how to fetch data, handle loading states, and process errors. This violates separation of concerns.

  3. Testing Complexity: How do you test this widget? You need to mock the future, control its completion timing, and handle all the snapshot states.

  4. Lifecycle Management: What if the user navigates away before the future completes? What about error recovery? Retry logic? These concerns pile up quickly.

A Better Approach: Separating Concerns

The solution is to move your asynchronous logic out of your widgets and into dedicated state management. Let’s look at a cleaner pattern using a simple state holder with ChangeNotifier.

Step 1: Create a Dedicated State Manager

import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';

class ProductState extends ChangeNotifier {
  Product? _product;
  bool _isLoading = false;
  String? _error;
  
  Product? get product => _product;
  bool get isLoading => _isLoading;
  String? get error => _error;
  
  Future<void> loadProduct(String productId) async {
    _isLoading = true;
    _error = null;
    notifyListeners();
    
    try {
      final response = await http.get(
        Uri.parse('https://api.example.com/products/$productId')
      );
      
      if (response.statusCode == 200) {
        _product = Product.fromJson(json.decode(response.body));
      } else {
        _error = 'Failed to load product';
      }
    } catch (e) {
      _error = 'Network error: $e';
    } finally {
      _isLoading = false;
      notifyListeners();
    }
  }
  
  void retry() {
    if (_error != null) {
      loadProduct('some-product-id');
    }
  }
}

Step 2: Use a State Management Wrapper

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class ProductPage extends StatelessWidget {
  final String productId;
  
  const ProductPage({super.key, required this.productId});
  
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => ProductState()..loadProduct(productId),
      child: Consumer<ProductState>(
        builder: (context, state, child) {
          if (state.isLoading) {
            return const Center(child: CircularProgressIndicator());
          }
          
          if (state.error != null) {
            return Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text('Error: ${state.error}'),
                  const SizedBox(height: 16),
                  ElevatedButton(
                    onPressed: state.retry,
                    child: const Text('Retry'),
                  ),
                ],
              ),
            );
          }
          
          if (state.product != null) {
            return ProductDetails(product: state.product!);
          }
          
          return const Center(child: Text('No product found'));
        },
      ),
    );
  }
}

Why This Approach Wins

  1. Single Responsibility: Your widget only builds UI. Your state class only manages state.

  2. Controlled Lifecycle: Data fetching happens once in initialization (through the provider’s create method), not on every build.

  3. Easy Testing: You can test your state logic independently from UI:

import 'package:flutter_test/flutter_test.dart';

void main() {
  test('ProductState loads product successfully', () async {
    final state = ProductState();
    await state.loadProduct('test-id');
    expect(state.product, isNotNull);
    expect(state.isLoading, false);
    expect(state.error, isNull);
  });
}
  1. Reusability: The same state can be used across multiple widgets without duplication.

  2. Better Error Handling: You can implement retry logic, caching, and other advanced features in one place.

When to Actually Use FutureBuilder/StreamBuilder

This doesn’t mean you should never use these widgets. They’re perfect for:

  • Simple one-off async operations that don’t need business logic
  • Prototyping and proof-of-concepts
  • Situations where you already have a future/stream from a state management solution

For example, after fetching data with our state manager:

Consumer<ProductState>(
  builder: (context, state, child) {
    return FutureBuilder<String>(
      future: state.product?.calculateShippingEstimate(),
      builder: (context, snapshot) {
        // Simple, focused async UI
      },
    );
  },
)

Taking It Further

For larger apps, consider established state management solutions:

  • Riverpod: Excellent type safety and testability
  • Bloc: Predictable state management with events and states
  • GetX: Lightweight with built-in dependency injection

Each provides patterns that naturally separate async logic from UI.

The Bottom Line

FutureBuilder and StreamBuilder are tools, not architectures. By moving your asynchronous logic out of your widgets and into dedicated state managers, you’ll build apps that are easier to test, maintain, and scale.

Start small: pick one screen in your app that uses FutureBuilder, extract its logic into a simple state class, and see the difference it makes.

This blog is produced with the assistance of AI by a human editor. Learn more

Related Posts

Cover image for Choosing the Right Storage for Flutter: Beyond SharedPreferences for Persistent Data

Choosing the Right Storage for Flutter: Beyond SharedPreferences for Persistent Data

Developers frequently struggle with persistent storage in Flutter, especially when dealing with OS-level backups and app reinstalls. This post will clarify the nuances of different storage solutions like `SharedPreferences`, `path_provider`, and `flutter_secure_storage`, focusing on how to correctly manage data lifetimes (per-install vs. per-user) and avoid issues like device IDs migrating to new phones or data being unexpectedly backed up.

Cover image for Optimizing Flutter UI Performance: Solving `ImageFiltered` Lag and Smooth Blurring

Optimizing Flutter UI Performance: Solving `ImageFiltered` Lag and Smooth Blurring

Flutter's `ImageFiltered` can cause significant lag when blurring large or frequently changing content like text. This post will explain the performance bottlenecks, provide solutions like caching blurred outputs with `RepaintBoundary` or `SnapshotWidget`, and offer practical tips to ensure smooth UI effects without sacrificing frame rates.