← Back to posts Cover image for Mastering Flutter UI Performance: Building JANK-Free Video Feeds and Complex Scrollables

Mastering Flutter UI Performance: Building JANK-Free Video Feeds and Complex Scrollables

· 5 min read
Weekly Digest

The Flutter news you actually need

No spam, ever. Unsubscribe in one click.

Chris
By Chris

Building silky-smooth video feeds and complex scrollable UIs in Flutter is a rite of passage. You start with a simple ListView.builder and a VideoPlayerController in each item, and suddenly your app is stuttering, devices are warming up, and scrolling feels like wading through molasses. The culprit is almost always the same: the costly creation and disposal of resources like video controllers during rapid scroll interactions.

Let’s break down why this happens and master the techniques to build jank-free experiences.

The Performance Pitfall: Thrashing Resources on Scroll

The most common mistake is initializing a VideoPlayerController inside a ListView item’s build method or initState. As users scroll, widgets are created and destroyed rapidly. If each creation triggers video controller initialization (involving platform channels and hardware decoder setup), and each disposal triggers cleanup, you create a performance death spiral. The garbage collector churns, the UI thread blocks waiting for platform operations, and jank spikes appear.

The Bad Pattern (Avoid This):

class JankyVideoItem extends StatefulWidget {
  @override
  _JankyVideoItemState createState() => _JankyVideoItemState();
}

class _JankyVideoItemState extends State<JankyVideoItem> {
  late VideoPlayerController _controller;

  @override
  void initState() {
    super.initState();
    // This runs every time the item is scrolled into view!
    _controller = VideoPlayerController.network(videoUrl)
      ..initialize().then((_) {
        setState(() {});
      });
  }

  @override
  void dispose() {
    // This runs every time the item is scrolled out of view!
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return VideoPlayer(_controller);
  }
}

Solution 1: The Video Player Pool Manager

The core optimization is to treat video controllers like a limited, reusable pool. Instead of creating/destroying on scroll, you pre-initialize a small number of controllers and reassign their data sources (video URLs) as needed. This keeps the expensive decoder setup/teardown to an absolute minimum.

Here’s a simplified manager pattern:

class VideoControllerPool {
  final Map<String, VideoPlayerController> _activeControllers = {};
  final List<VideoPlayerController> _idlePool = [];
  final int _maxPoolSize = 3; // Typically 1-2 more than visible items

  Future<VideoPlayerController> getControllerForUrl(String url) async {
    // Return existing controller if already active for this URL
    if (_activeControllers.containsKey(url)) {
      return _activeControllers[url]!;
    }

    // Reuse an idle controller from the pool
    VideoPlayerController controller;
    if (_idlePool.isNotEmpty) {
      controller = _idlePool.removeLast();
      await controller.pause();
    } else if (_activeControllers.length < _maxPoolSize) {
      // Create a new one if under pool limit
      controller = VideoPlayerController.network('');
    } else {
      // Recycle the least recently used active controller
      final lruUrl = _activeControllers.keys.first;
      controller = _activeControllers[lruUrl]!;
      _activeControllers.remove(lruUrl);
      await controller.pause();
    }

    // Reconfigure the controller with the new URL
    await controller.setNetworkDataSource(url);
    _activeControllers[url] = controller;
    return controller;
  }

  void releaseController(String url) {
    final controller = _activeControllers[url];
    if (controller != null) {
      _activeControllers.remove(url);
      // Keep it in the idle pool for future reuse
      _idlePool.add(controller);
    }
  }

  void disposeAll() {
    [..._activeControllers.values, ..._idlePool].forEach((c) => c.dispose());
    _activeControllers.clear();
    _idlePool.clear();
  }
}

In your widget, you’d call pool.getControllerForUrl when an item becomes visible (using VisibilityDetector or within a PageView/ListView visibility callback) and pool.releaseController when it’s far off-screen.

Solution 2: Precise Control with ValueNotifier and AutomaticKeepAliveClientMixin

For non-pooled scenarios or other expensive widgets, combine AutomaticKeepAliveClientMixin to prevent disposal with a ValueNotifier to manage state efficiently.

class OptimizedListItem extends StatefulWidget {
  final String videoUrl;

  const OptimizedListItem({super.key, required this.videoUrl});

  @override
  State<OptimizedListItem> createState() => _OptimizedListItemState();
}

class _OptimizedListItemState extends State<OptimizedListItem>
    with AutomaticKeepAliveClientMixin {
  late final VideoPlayerController _controller;
  final _isInitialized = ValueNotifier<bool>(false);

  @override
  bool get wantKeepAlive => _isInitialized.value; // Keep alive once initialized

  @override
  void initState() {
    super.initState();
    _controller = VideoPlayerController.network(widget.videoUrl)
      ..initialize().then((_) {
        _isInitialized.value = true; // Triggers keep-alive
      });
  }

  @override
  Widget build(BuildContext context) {
    super.build(context); // Required for AutomaticKeepAliveClientMixin
    return ValueListenableBuilder<bool>(
      valueListenable: _isInitialized,
      builder: (context, isInitialized, child) {
        return isInitialized
            ? VideoPlayer(_controller)
            : const Center(child: CircularProgressIndicator());
      },
    );
  }

  @override
  void dispose() {
    _controller.dispose();
    _isInitialized.dispose();
    super.dispose();
  }
}

Solution 3: Offloading Work and Smooth Scrolling

For complex scrollables with heavy layouts, ensure you’re not building expensive widgets during scroll animations.

  1. Use const constructors wherever possible.
  2. Lazily load content: Defer non-essential image decoding or data parsing until after the scroll settles.
  3. Offload computations: Use compute or Isolate for heavy synchronous tasks that block the UI thread. Don’t assume async alone makes something non-blocking.
Future<void> _loadAndProcessData() async {
  // This async operation still blocks the UI thread during processing!
  final heavyData = await _fetchData();
  // Move intensive processing to a separate isolate
  final result = await compute(_processDataHeavily, heavyData);
  setState(() => _data = result);
}

// This function runs in a separate isolate
static HeavyResult _processDataHeavily(RawData input) {
  // ... intensive CPU work ...
  return HeavyResult();
}

Key Takeaways

  • Never create/dispose VideoPlayerController in rapid succession. Use a pooling or caching strategy.
  • Limit the number of active video players to just those visibly playing (plus one buffer).
  • Use AutomaticKeepAliveClientMixin strategically for expensive widget state.
  • Profile with the Flutter Performance overlay and the DevTools timeline to identify raster and UI thread jank.
  • Remember async doesn’t mean non-blocking for CPU work—use isolates.

By treating controllers as precious resources and managing their lifecycle proactively, you can build Flutter video feeds that are indistinguishable from native performance, even on budget hardware. The goal is to do less work on the critical path of the UI thread, and these patterns provide a solid foundation for exactly that.

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

Related Posts

Cover image for Flutter's Hidden Gem: Building Powerful Linux Desktop Apps with Ease

Flutter's Hidden Gem: Building Powerful Linux Desktop Apps with Ease

Flutter is gaining traction as a robust framework for Linux desktop development, offering a smoother experience compared to traditional toolkits like GTK and Qt. This post will explore why Flutter excels for Linux apps, highlight its advantages, and provide practical insights for developers looking to leverage Flutter for their desktop projects.

Cover image for Simplifying Flutter Desktop Deployment: Signing and Distribution for Windows

Simplifying Flutter Desktop Deployment: Signing and Distribution for Windows

Deploying Flutter desktop apps on Windows can be challenging, especially regarding code signing and preventing Windows from blocking installations. This post will guide developers through the process of signing their Flutter desktop applications, exploring alternatives to the Microsoft Store like creating executable installers, and covering the necessary steps to ensure a smooth user experience.

Cover image for Mastering Flutter Tooling: Streamlining SDK Management and Installation on Windows

Mastering Flutter Tooling: Streamlining SDK Management and Installation on Windows

Developers frequently voice frustrations about Flutter SDK version management and installation processes, particularly on Windows. This post will explore best practices for managing multiple Flutter versions using tools like FVM, discuss integrating Flutter with package managers like Winget for a smoother Windows setup, and offer strategies to ensure a consistent development environment across projects and teams.