Flutter assertion: ScrollController attached to multiple scroll views
ScrollController attached to multiple scroll views.Short answer
The assertion fires when code reads the controller's single position, such as offset, jumpTo or animateTo, while two scrollables are attached to it. Either you passed one controller to two widgets, or two lists without controllers both grabbed the primary one. Give each list its own controller, or set primary to false.
Fix it fast
1Find out which cause you have
List every place a ScrollController is created or passed, then check which of those widgets are on screen at the same time.
grep -rn "ScrollController\|primary:" lib2Match what you see, then jump to its fix
- 1
The same variable appears in the controller argument of two ListView, GridView or SingleChildScrollView widgetsOne controller is passed to two scrollablesFix - 2
No explicit controller is shared, but two vertical lists are visible at once, typically on web or desktopTwo lists with no controller inherit the same primary controllerFix
- 1
What does the error look like?
ScrollController attached to multiple scroll views.'package:flutter/src/widgets/scroll_controller.dart': Failed assertion: '_positions.length == 1'
Cause 1 of 2
One controller is passed to two scrollables
1Check
A controller makes one ScrollPosition per attached scrollable. Reading offset or calling jumpTo is only valid when exactly one position is attached, and positions lists them all.
grep -rn "controller: _\w*[cC]ontroller" lib2Fix
Create one controller per scrollable. If you want two lists to move together, listen to one and jump the other, instead of sharing the object.
final _leftController = ScrollController();
final _rightController = ScrollController();
ListView(controller: _leftController, children: left)
ListView(controller: _rightController, children: right)3Verify
Open the screen, scroll both lists, and trigger the code that read offset; no assertion appears.
flutter runConfirmed by api.flutter.dev
Cause 2 of 2
Two lists with no controller inherit the same primary controller
1Check
A scroll view without its own controller can inherit the PrimaryScrollController of its route. Flutter's common-errors page says the clash is more likely on web and desktop than on mobile.
grep -rn "PrimaryScrollController" lib2Fix
Mark the secondary list as not primary, or give it its own controller. A scroll view cannot be primary and have a controller at the same time.
ListView(
primary: false,
children: items,
)3Verify
Resize the window or run on web so both lists show, then scroll each one.
flutter run -d chromeConfirmed by api.flutter.dev
How do I stop it coming back?
- Give every scrollable that shows together its own controller, and dispose each one.
- Set primary: false on nested or side-by-side lists.
- Test list screens on web or desktop, where several lists are easier to show at once.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Mobile apps
AudioCache has no play method after updating audioplayers
The method 'play' isn't defined for the type 'AudioCache'.
Since audioplayers 1.0, AudioCache only turns asset files into local files and caches them.
2 causes, each with a fixMobile apps
Flutter Bloc throws that emit was called after the event handler completed
emit was called after an event handler completed normally.
Each on<Event> handler gets its own Emitter, and it stops working the moment the handler's returned Future completes.
2 causes, each with a fixMobile apps
analysis_options.yaml: the URI it includes can't be found
include_file_not_found
The analyzer reports include_file_not_found when the file named after include: in analysis_options.yaml does not resolve.
2 causes, each with a fixMobile apps
Dart error: the body might complete normally and return null for a non-nullable type
body_might_complete_normally
Dart found a path through your function or callback that reaches the closing brace without a return.
3 causes, each with a fixMobile apps
Dart throws ConcurrentModificationError while looping over a list
ConcurrentModificationError
Dart throws this when a collection changes size while something iterates it.
2 causes, each with a fixMobile apps
Dart cannot turn a compact timestamp like 20201101153000 into a DateTime
FormatException: Invalid date format
DateTime.parse accepts a subset of ISO 8601, and a time part must be introduced by T or a space.
2 causes, each with a fix