Skip to content

Comments

Fix setState() called after dispose() in UncontrolledProviderScope#4681

Open
itsatifsiddiqui wants to merge 1 commit intorrousselGit:masterfrom
itsatifsiddiqui:fix/provider-scope-setstate-after-dispose
Open

Fix setState() called after dispose() in UncontrolledProviderScope#4681
itsatifsiddiqui wants to merge 1 commit intorrousselGit:masterfrom
itsatifsiddiqui:fix/provider-scope-setstate-after-dispose

Conversation

@itsatifsiddiqui
Copy link

@itsatifsiddiqui itsatifsiddiqui commented Feb 4, 2026

Related Issues

fixes #4661

Description

This PR fixes the setState() called after dispose() error that occurs when ProviderScope instances are used in scrollable lists (e.g., ListView.builder, SliverList).

Root Cause

The _UncontrolledProviderScopeState.build() method schedules a frame callback without checking if the widget is still mounted:

WidgetsBinding.instance.scheduleFrameCallback(scheduleNewFrame: false, (_) {
  setState(() {});
});

When scrolling quickly, widgets get disposed but the scheduled callbacks still fire, causing the error.

Solution

Added a mounted check before calling setState():

WidgetsBinding.instance.scheduleFrameCallback(scheduleNewFrame: false, (_) {
  if (mounted) {
    setState(() {});
  }
});

Testing

Tested in a production Flutter app with:

  • ListView.builder containing widgets wrapped in ProviderScope
  • No more setState() called after dispose() errors

Checklist

  • I have updated the CHANGELOG.md of the relevant packages.
    Changelog files must be edited under the form:

    ## Unreleased fix
    
    - Fixed \`setState() called after dispose()\` error in \`UncontrolledProviderScope\` when used in scrollable lists (thanks to @itsatifsiddiqui)
  • If this contains new features or behavior changes,
    I have updated the documentation to match those changes.
    (Not applicable - this is a bug fix with no behavior changes)

Summary by CodeRabbit

Bug Fixes

  • Fixed "setState() called after dispose()" error that occurred when using UncontrolledProviderScope within scrollable lists.

Adds a mounted check before calling setState() in the frame callback
scheduled by UncontrolledProviderScope.build(). This prevents the error
when ProviderScope instances are used in scrollable lists and get
disposed while frame callbacks are still pending.

Fixes rrousselGit#4661

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 4, 2026

📝 Walkthrough

Walkthrough

The PR fixes a regression in UncontrolledProviderScope where setState() was called on disposed widgets during frame callbacks, causing errors when ProviderScope widgets are disposed (e.g., during PageView navigation). A mounted check is added to guard the setState invocation. The CHANGELOG documents this fix.

Changes

Cohort / File(s) Summary
Documentation
packages/flutter_riverpod/CHANGELOG.md
Added unreleased fix entry documenting the setState() after dispose() error resolution.
Core Bug Fix
packages/flutter_riverpod/lib/src/core/provider_scope.dart
Added mounted check before setState in frame callback to prevent setState calls on unmounted UncontrolledProviderScope widgets.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

Suggested reviewers

  • rrousselGit

Poem

🐰 A mounted check, so small yet true,
Stops setState on the widgets through,
No more errors when they're disposed and gone,
The scope stays calm, the fix lives on! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The PR title accurately reflects the main change: fixing the 'setState() called after dispose()' error in UncontrolledProviderScope, which is the primary objective addressed in the changeset.
Linked Issues check ✅ Passed The PR successfully implements the core objective from issue #4661 by adding a mounted check to prevent setState() calls after dispose() in frame callbacks [#4661].
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the setState() after dispose() issue; CHANGELOG documentation and provider_scope.dart modifications align with the stated objectives.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[3.2.0] [flutter_riverpod] Regression in rebuilding ProviderScope at the start of every frame

1 participant