Skip to content

Error when remove ScrollView, which has a ScrollController, from widget tree #82573

Open

Description

@xuanswe

Steps to Reproduce

The example below use LayoutBuilder to show Scrollbar when screen width is smaller than 960dp and remove the Scrollbar when larger than 960dp.

If we want isAlwaysShown: true, we must provide the same ScrollController to both Scrollbar and ScrollView.
When remove the ScrollView from the current widget tree, an exception is thrown.

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(home: MyStatelessWidget());
  }
}

class MyStatelessWidget extends StatefulWidget {
  const MyStatelessWidget({Key? key}) : super(key: key);

  @override
  _MyStatelessWidgetState createState() => _MyStatelessWidgetState();
}

class _MyStatelessWidgetState extends State<MyStatelessWidget> {
  ScrollController _scrollController = ScrollController();

  @override
  Widget build(BuildContext context) {
    Widget _scaffold = Scaffold(
      appBar: AppBar(
        title: const Text('LayoutBuilder Example'),
      ),
      body: Container(
        child: Text(
          'Example Content',
          style: TextStyle(fontSize: 200),
        ),
      ),
    );
    return LayoutBuilder(builder: (
      BuildContext context,
      BoxConstraints constraints,
    ) {
      Widget _result = _scaffold;
      if (constraints.minWidth < 960) {
        _result = wrapWithWidthScroll(_result);
      }
      return _result;
    });
  }

  Widget wrapWithWidthScroll(Widget _scaffold) {
    return Scrollbar(
      isAlwaysShown: true,
      controller: _scrollController,
      child: SingleChildScrollView(
        controller: _scrollController,
        child: Container(width: 960, child: _scaffold),
        scrollDirection: Axis.horizontal,
      ),
    );
  }
}

Expected results:

No error when we expand screen width (web or desktop) from ex. 900dp to ex. 1000dp.

Actual results:
Error when we expand screen width (web or desktop) from ex. 900dp to ex. 1000dp.

======== Exception caught by scheduler library =====================================================
The following assertion was thrown during a scheduler callback:
The Scrollbar's ScrollController has no ScrollPosition attached.

A Scrollbar cannot be painted without a ScrollPosition. 

The Scrollbar attempted to use the PrimaryScrollController. This ScrollController should be associated with the ScrollView that the Scrollbar is being applied to. When providing your own ScrollController, ensure both the Scrollbar and the Scrollable widget use the same one.

When the exception was thrown, this was the stack: 
#0      RawScrollbarState._maybeTriggerScrollbar.<anonymous closure>.<anonymous closure> (package:flutter/src/widgets/scrollbar.dart:960:13)
#1      RawScrollbarState._maybeTriggerScrollbar.<anonymous closure> (package:flutter/src/widgets/scrollbar.dart:985:10)
#2      SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1144:15)
#3      SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1090:9)
#4      SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:998:5)
...
====================================================================================================

flutter doctor -v

[√] Flutter (Channel beta, 2.2.0-10.3.pre, on Microsoft Windows [Version 10.0.19042.964], locale en-DE)
    • Flutter version 2.2.0-10.3.pre at C:\...\flutter
    • Framework revision 06e2fd6357 (5 days ago), 2021-05-08 11:28:22 -0700
    • Engine revision a123e75c60
    • Dart version 2.13.0 (build 2.13.0-211.14.beta)

Activity

  1. added
    f: scrolling
    framework
    c: crash
    on May 14, 2021
  2. goderbauer commented on May 19, 2021

    @goderbauer
    Member
  3. added
    d: stackoverflow
    and removed
    c: crash
    on Jun 4, 2021
  4. Piinks commented on Jun 4, 2021

    @Piinks
    Contributor

    @nguyenxndaidev can you not remove the Scrollbar widget when you remove the ScrollView or toggle the isAlwaysShown? When you remove the ScrollView and leave the Scrollbar there as it is, the Scrollbar expects that you have linked it to a ScrollView. The Scrollbar tries to paint itself since you have isAlwaysShown true but there is no Scrollable, so it fails to do so.

    You could also use ScrollBehavior to apply the Scrollbar instead. That way you do not have to add a Scrollbar. Instead, the Scrollable itself will get a Scrollbar from the ScrollBehavior.buildScrollbar. So if you remove the ScrollView, the Scrollbar goes away too.

  5. added
    waiting for response
    on Jun 4, 2021
  6. xuanswe commented on Jun 11, 2021

    @xuanswe
    Author

    @nguyenxndaidev can you not remove the Scrollbar widget when you remove the ScrollView or toggle the isAlwaysShown? When you remove the ScrollView and leave the Scrollbar there as it is, the Scrollbar expects that you have linked it to a ScrollView. The Scrollbar tries to paint itself since you have isAlwaysShown true but there is no Scrollable, so it fails to do so.

    I am not quite sure if I understand you correctly. In the provided snippet, both Scrollbar and ScrollView are removed if constraints.minWidth >= 960.

    You could also use ScrollBehavior to apply the Scrollbar instead. That way you do not have to add a Scrollbar. Instead, the Scrollable itself will get a Scrollbar from the ScrollBehavior.buildScrollbar. So if you remove the ScrollView, the Scrollbar goes away too.

    I will try this solution when I have time and consider it as a workaround. We should still need to fix this current issue, right?

  7. removed
    waiting for response
    on Jun 11, 2021
  8. xuanswe commented on Jun 20, 2021

    @xuanswe
    Author

    @nguyenxndaidev can you not remove the Scrollbar widget when you remove the ScrollView or toggle the isAlwaysShown? When you remove the ScrollView and leave the Scrollbar there as it is, the Scrollbar expects that you have linked it to a ScrollView. The Scrollbar tries to paint itself since you have isAlwaysShown true but there is no Scrollable, so it fails to do so.

    If we try to not remove the Scrollbar widget:

    import 'package:flutter/material.dart';
    
    void main() => runApp(const MyApp());
    
    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return const MaterialApp(home: MyStatelessWidget());
      }
    }
    
    class MyStatelessWidget extends StatefulWidget {
      const MyStatelessWidget({Key? key}) : super(key: key);
    
      @override
      _MyStatelessWidgetState createState() => _MyStatelessWidgetState();
    }
    
    class _MyStatelessWidgetState extends State<MyStatelessWidget> {
      ScrollController _scrollController = ScrollController();
    
      @override
      Widget build(BuildContext context) {
        Widget _scaffold = Scaffold(
          appBar: AppBar(
            title: const Text('LayoutBuilder Example'),
          ),
          body: Container(
            child: Text(
              'Example Content',
              style: TextStyle(fontSize: 200),
            ),
          ),
        );
    
        return LayoutBuilder(
          builder: (
            BuildContext context,
            BoxConstraints constraints,
          ) {
            Widget scrollChild = Container(child: _scaffold);
    
            if (constraints.minWidth < 960)
              scrollChild = SingleChildScrollView(
                controller: _scrollController,
                child: Container(width: 960, child: _scaffold),
                scrollDirection: Axis.horizontal,
              );
    
            return Scrollbar(
              isAlwaysShown: true,
              controller: _scrollController,
              child: scrollChild,
            );
          },
        );
      }
    }
    

    We will get this error:

    ======== Exception caught by scheduler library =====================================================
    The following assertion was thrown during a scheduler callback:
    The Scrollbar's ScrollController has no ScrollPosition attached.
    
    A Scrollbar cannot be painted without a ScrollPosition. 
    
    The Scrollbar attempted to use the PrimaryScrollController. This ScrollController should be associated with the ScrollView that the Scrollbar is being applied to. When providing your own ScrollController, ensure both the Scrollbar and the Scrollable widget use the same one.
    
    When the exception was thrown, this was the stack: 
    #0      RawScrollbarState._maybeTriggerScrollbar.<anonymous closure>.<anonymous closure> (package:flutter/src/widgets/scrollbar.dart:960:13)
    #1      RawScrollbarState._maybeTriggerScrollbar.<anonymous closure> (package:flutter/src/widgets/scrollbar.dart:985:10)
    #2      SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1144:15)
    #3      SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1090:9)
    #4      SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:865:7)
    ...
    ====================================================================================================
    
  9. xuanswe commented on Jun 20, 2021

    @xuanswe
    Author

    Oh, but if isAlwaysShown: constraints.minWidth < 960 ? true : false, it works.
    Even though it works, it's just a workaround I think.

    I need to test this workaround more for the 2D case.

  10. xuanswe commented on Jun 20, 2021

    @xuanswe
    Author

    I couldn't make it work for the 2D case with this workaround. Or maybe it will work but will be too complex.

  11. added
    P2
    on Apr 20, 2022
  12. added
    team-framework
    triaged-framework
    on Jul 8, 2023
  13. ibrahimsebbane commented on Dec 29, 2023

    @ibrahimsebbane

    Guys in my Case I have ScrollBar with child ScollablePositionedList instead of ScrollView what should I do ? it doesnt have the parameter controller (ScrollView doesn't have this)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2d: stackoverflowf: scrollingframeworkteam-frameworktriaged-framework

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions