Description
Statement Of Problem
Hi, reporting an intermittent problem with scrollable_positioned_list: ^0.1.8 (both Android and iOS). In my release build it causes random lock-ups so I can't release my app at present.
To trigger: Send app to background while in landscape , rotate the phone, lock the orientation, wait around 60 seconds then wake the app in portrait. Flutter will attempt to rotate to portrait but will freeze with the previous landscape screen content rotated and scaled to portrait.
Debugging gives this error
_AssertionError ('package:flutter/src/widgets/scroll_controller.dart':
Failed assertion: line 112 pos 12: '_positions.isNotEmpty': ScrollController not attached to any scroll views.)
The call stack is
_AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:46)
_AssertionError._throwNew (dart:core-patch/errors_patch.dart:36)
ScrollController.position (/users/markjenkins/applications/flutter/packages/flutter/lib/src/widgets/scroll_controller.dart:112)
_ScrollablePositionedListState._startScroll.<anonymous closure>.<anonymous closure> (/users/markjenkins/applications/flutter/.pub-cache/hosted/pub.dartlang.org/scrollable_positioned_list-0.1.8/lib/src/scrollable_positioned_list.dart:452)
SchedulerBinding._invokeFrameCallback (/users/markjenkins/applications/flutter/packages/flutter/lib/src/scheduler/binding.dart:1117)
SchedulerBinding.handleDrawFrame (/users/markjenkins/applications/flutter/packages/flutter/lib/src/scheduler/binding.dart:1063)
SchedulerBinding._handleDrawFrame (/users/markjenkins/applications/flutter/packages/flutter/lib/src/scheduler/binding.dart:971)
SchedulerBinding._handleDrawFrame (/users/markjenkins/applications/flutter/packages/flutter/lib/src/scheduler/binding.dart:0)
_rootRun (dart:async/zone.dart:1190)
_rootRun (dart:async/zone.dart:0)
_CustomZone.run (dart:async/zone.dart:1093)
_CustomZone.runGuarded (dart:async/zone.dart:997)
_invoke (dart:ui/hooks.dart:251)
_drawFrame (dart:ui/hooks.dart:209)
This is the affected code
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';
import 'package:NexTrain/UI/Common/Dimensions.dart';
/* -- Class --------------------------------------------------------------------------------- */
class DestinationScroller extends StatefulWidget {
final List<Widget> items;
DestinationScroller({this.items});
@override
State createState() => new _DestinationScrollerState();
}
/* -- State --------------------------------------------------------------------------------- */
class _DestinationScrollerState extends State<DestinationScroller> {
ItemScrollController _itemScrollController = ItemScrollController();
final dimensions = new Dimensions();
int _index = 0;
int _maxIndex = 0;
Timer _timer;
@override
void initState() {
super.initState();
_maxIndex = widget.items.length - 1;
// Scroll if more than one item
if (_maxIndex > 0) {
_timer = Timer.periodic(
Duration(seconds: 4),
(Timer t) => _scrollToNext(),
);
// Otherwise kill existing timer
} else {
_timer?.cancel();
}
}
@override
void dispose() {
_timer?.cancel();
super.dispose();
}
/* -- Class methods ---------------------------------------------------------------------------- */
_scrollToNext() {
// Move smoothly to next. At end of list jump to start
if (_index < _maxIndex) {
_index++;
_itemScrollController.scrollTo(
index: _index,
duration: Duration(milliseconds: 450),
curve: Curves.easeOut,
);
} else {
_index = 0;
_itemScrollController.jumpTo(index: _index);
}
}
/* -- Builder ----------------------------------------------------------------------------------- */
@override
Widget build(BuildContext context) {
return IgnorePointer(
child: SizedBox(
height:
dimensions.boardRowHeight + dimensions.landscapeBodyTopSpacerHeight,
child: ScrollablePositionedList.builder(
itemCount: widget.items.length,
itemScrollController: _itemScrollController,
itemBuilder: (context, index) => widget.items[index],
),
),
);
}
}
Possible cause/fix
I'm pretty sure I know what the problem is, covered by this link:
https://stackoverflow.com/questions/52114535/scrollcontroller-not-attached-to-any-scroll-views
The recommended solution is to check hasClients before referencing the controller (to call scrollTo or JumpTo) eg:
if (_scrollController.hasClients)
_scrollController.jumpTo(50.0);
I can't check hasClients myself as the Widget doesn't expose it. I can see that v0.1.8 doesn't reference the hasClients method, so hence my guess that this is the cause.
Flutter doctor -v
[✓] Flutter (Channel stable, 1.22.2, on Mac OS X 10.15.5 19F101, locale en-GB)
• Flutter version 1.22.2 at /users/markjenkins/applications/flutter
• Framework revision 84f3d28555 (2 weeks ago), 2020-10-15 16:26:19 -0700
• Engine revision b8752bbfff
• Dart version 2.10.2
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
• Android SDK at /Users/markjenkins/Library/Android/sdk
• Platform android-29, build-tools 29.0.3
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 12.0)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 12.0, Build version 12A7209
• CocoaPods version 1.9.1
[✓] Android Studio (version 4.0)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin version 48.0.2
• Dart plugin version 193.7361
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
[✓] VS Code (version 1.50.1)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.15.1
[✓] Connected device (1 available)
• Mark’s iPhone X (mobile) • 99fd963188a45f5e8894bad482ba7345e985280d • ios • iOS 14.0