diff --git a/packages/flutter/lib/src/cupertino/scrollbar.dart b/packages/flutter/lib/src/cupertino/scrollbar.dart index fde870df4a..b0112ef7d2 100644 --- a/packages/flutter/lib/src/cupertino/scrollbar.dart +++ b/packages/flutter/lib/src/cupertino/scrollbar.dart @@ -185,7 +185,10 @@ class _CupertinoScrollbarState extends RawScrollbarState { @override void handleThumbPressStart(Offset localPosition) { super.handleThumbPressStart(localPosition); - final Axis direction = getScrollbarDirection()!; + final Axis? direction = getScrollbarDirection(); + if (direction == null) { + return; + } switch (direction) { case Axis.vertical: _pressStartAxisPosition = localPosition.dy; diff --git a/packages/flutter/test/cupertino/scrollbar_test.dart b/packages/flutter/test/cupertino/scrollbar_test.dart index 28a92dc9d9..d5631dafd9 100644 --- a/packages/flutter/test/cupertino/scrollbar_test.dart +++ b/packages/flutter/test/cupertino/scrollbar_test.dart @@ -1060,6 +1060,50 @@ void main() { ); }); + testWidgets('Throw if interactive with the bar when no position attached', (WidgetTester tester) async { + final ScrollController scrollController = ScrollController(); + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: MediaQuery( + data: const MediaQueryData(), + child: CupertinoScrollbar( + controller: scrollController, + thumbVisibility: true, + child: SingleChildScrollView( + controller: scrollController, + child: const SizedBox( + height: 1000.0, + width: 1000.0, + ), + ), + ), + ), + ), + ); + + await tester.pumpAndSettle(); + + final ScrollPosition position = scrollController.position; + scrollController.detach(position); + + final FlutterExceptionHandler? handler = FlutterError.onError; + FlutterErrorDetails? error; + FlutterError.onError = (FlutterErrorDetails details) { + error = details; + }; + + // long press the thumb + await tester.startGesture(const Offset(796.0, 50.0)); + await tester.pump(kLongPressDuration); + + expect(error, isNotNull); + + scrollController.attach(position); + FlutterError.onError = handler; + }); + testWidgets('Interactive scrollbars should have a valid scroll controller', (WidgetTester tester) async { final ScrollController primaryScrollController = ScrollController(); final ScrollController scrollController = ScrollController();