Skip to content

Bug: Modifying Provider State during the Build Phase in Channel Details #8

@windoze95

Description

@windoze95

File: lib/screens/channel_detail_screen.dart
Line: ~79 (in build)

Description:
The ChannelDetailScreen calls _checkPollingNeeded(videos) inside its build() method via videosAsync.whenData().
Because videosAsync.whenData() directly invokes the callback with the resolved value during the build phase itself, this executes _pendingVideoIds.remove(id) and _startPolling()/_stopPolling() directly inside build(), which modifies widget state and starts timers outside the proper lifecycle hooks.
This is an anti-pattern and can cause race conditions or unhandled Flutter errors (setState() or markNeedsBuild() called during build).

Impact:
Unpredictable rebuild loops, UI lag, and hard-to-track polling bugs in the channel details view.

Suggested Fix:
Use ref.listen inside the build method rather than whenData, or execute the polling checks in a separate Riverpod Notifier completely decoupled from the widget layer.

ref.listen<AsyncValue<List<Video>>>(channelVideosProvider(widget.channelId), (prev, next) {
  next.whenData((videos) => _checkPollingNeeded(videos));
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions