Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"setState() or markNeedsBuild() called during build" error combining DraggableScrollableSheet with FloatingActionButton #146736

Open
alexmcmanus opened this issue Apr 14, 2024 · 4 comments
Labels
a: error message Error messages from the Flutter framework found in release: 3.19 Found to occur in 3.19 found in release: 3.22 Found to occur in 3.22 framework flutter/packages/flutter repository. See also f: labels. has reproducible steps The issue has been confirmed reproducible and is ready to work on P2 Important issues not at the top of the work list team-framework Owned by Framework team triaged-framework Triaged by Framework team

Comments

@alexmcmanus
Copy link

Steps to reproduce

I have a Scaffold with a bottomNavigationBar modifying a _pageIndex state variable.
For pageIndex == 1:

  • the Scaffold has a bottomSheet sheet set to a widget containing a DraggableScrollableSheet, with a button that when clicked, uses the DraggableScrollableController.animateTo() method to expand the bottom sheet.
  • the Scaffold has a floatingActionButton set.

To reproduce (running in the iOS simulator):

  • Select page 1 in the bottom nav bar
  • Click the "Toggle" button in the bottom sheet, which should expand to 50%
  • Select page 0 in the bottom nav bar

Expected results

It switches back to Page 0, the bottom sheet and floating action button disappear, and there are no errors in the terminal.

Actual results

Visually it works fine, but there is the following error in the debug console (which appear to be originating in the framework):

The following assertion was thrown while notifying listeners for TrainHoppingAnimation:
setState() or markNeedsBuild() called during build.
This RotationTransition widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was: RotationTransition
    listenable: AnimationController#31171(⏭ 1.000; paused)➩CurveTween(curve: Cubic(0.42, 0.00, 1.00, 1.00))➩Tween<double>(0.875 → 1.0)➩1.0➩TrainHoppingAnimation(next: _AnimationSwap<double>(AnimationController#a27a8(⏭ 1.000; paused)➩Tween<double>(0.75 → 1.0)➩1.0, AnimationController#a27a8(⏭ 1.000; paused)➩CurveTween(curve: Threshold)➩1.0➪ReverseAnimation))
    state: _AnimatedState#bb51b
The widget which was currently being built when the offending call was made was: DraggableScrollableSheet
    dirty
    dependencies: [_InheritedResetNotifier, _InheritedTheme, _LocalizationsScope-[GlobalKey#46482]]
    state: _DraggableScrollableSheetState#00283

When the exception was thrown, this was the stack:
#0      Element.markNeedsBuild.<anonymous closure> (package:flutter/src/widgets/framework.dart:5042:9)
#1      Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:5054:6)
#2      State.setState (package:flutter/src/widgets/framework.dart:1223:15)
#3      _AnimatedState._handleChange (package:flutter/src/widgets/transitions.dart:129:5)
#4      AnimationLocalListenersMixin.notifyListeners (package:flutter/src/animation/listener_helpers.dart:161:19)
#5      TrainHoppingAnimation._valueChangeHandler (package:flutter/src/animation/animations.dart:582:7)
#6      AnimationLocalListenersMixin.notifyListeners (package:flutter/src/animation/listener_helpers.dart:161:19)
#7      AnimationController.value= (package:flutter/src/animation/animation_controller.dart:395:5)
#8      ScaffoldState._floatingActionButtonVisibilityValue= (package:flutter/src/material/scaffold.dart:2532:47)
#9      _StandardBottomSheetState.extentChanged (package:flutter/src/material/scaffold.dart:3224:16)
#10     _NotificationElement.onNotification (package:flutter/src/widgets/notification_listener.dart:130:38)
#11     _NotificationNode.dispatchNotification (package:flutter/src/widgets/framework.dart:3320:18)
#12     _NotificationNode.dispatchNotification (package:flutter/src/widgets/framework.dart:3323:13)
#13     _NotificationNode.dispatchNotification (package:flutter/src/widgets/framework.dart:3323:13)
#14     _NotificationNode.dispatchNotification (package:flutter/src/widgets/framework.dart:3323:13)
#15     _NotificationNode.dispatchNotification (package:flutter/src/widgets/framework.dart:3323:13)
#16     Element.dispatchNotification (package:flutter/src/widgets/framework.dart:4942:24)
#17     Notification.dispatch (package:flutter/src/widgets/notification_listener.dart:60:13)
#18     _DraggableSheetExtent.updateSize (package:flutter/src/widgets/draggable_scrollable_sheet.dart:581:7)
#19     _DraggableScrollableSheetScrollController.reset (package:flutter/src/widgets/draggable_scrollable_sheet.dart:825:12)
#20     _DraggableScrollableSheetState.didChangeDependencies (package:flutter/src/widgets/draggable_scrollable_sheet.dart:680:25)
#21     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5640:13)
#22     Element.rebuild (package:flutter/src/widgets/framework.dart:5196:7)
#23     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2904:19)
#24     WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:989:21)
#25     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:448:5)
#26     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1386:15)
#27     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1311:9)
#28     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1169:5)
#29     _invoke (dart:ui/hooks.dart:312:13)
#30     PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:399:5)
#31     _drawFrame (dart:ui/hooks.dart:283:31)

The TrainHoppingAnimation notifying listeners was: AnimationController#31171(⏭ 1.000; paused)➩CurveTween(curve: Cubic(0.42, 0.00, 1.00, 1.00))➩Tween<double>(0.875 → 1.0)➩1.0➩TrainHoppingAnimation(next: _AnimationSwap<double>(AnimationController#a27a8(⏭ 1.000; paused)➩Tween<double>(0.75 → 1.0)➩1.0, AnimationController#a27a8(⏭ 1.000; paused)➩CurveTween(curve: Threshold)➩1.0➪ReverseAnimation))

Code sample

Code sample
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _pageIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Theme.of(context).colorScheme.inversePrimary,
          title: Text(widget.title),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                'Page Index: ${this._pageIndex}',
                style: Theme.of(context).textTheme.headlineMedium,
              ),
            ],
          ),
        ),
        bottomNavigationBar: NavigationBar(
          selectedIndex: _pageIndex,
          onDestinationSelected: (int index) {
            setState(() {
              _pageIndex = index;
            });
          },
          destinations: const [
            NavigationDestination(
              label: 'Page 0',
              icon: Icon(Icons.first_page),
            ),
            NavigationDestination(
              label: 'Page 1',
              icon: Icon(Icons.last_page),
            ),
          ],
        ),
        bottomSheet: _pageIndex == 1 ? MyBottomSheet() : null,
        floatingActionButton: _pageIndex == 1
            ? Builder(builder: (context) {
                return FloatingActionButton(
                    onPressed: () {
                      print('FAB shopping press');
                    },
                    child: const Icon(Icons.add));
              })
            : null);
  }
}

class MyBottomSheet extends StatefulWidget {
  const MyBottomSheet({super.key});

  @override
  State<StatefulWidget> createState() => _MyBottomSheetState();
}

class _MyBottomSheetState extends State<MyBottomSheet> {
  late DraggableScrollableController _controller;
  bool expanded = false;

  @override
  void initState() {
    super.initState();
    _controller = DraggableScrollableController();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return DecoratedBox(
      decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.vertical(
          top: Radius.circular(0.0),
        ),
      ),
      child: DraggableScrollableSheet(
        expand: false,
        snap: true,
        initialChildSize: 0.25,
        maxChildSize: 0.5,
        snapSizes: [0.5],
        controller: _controller,
        builder: (context, scrollController) {
          return Stack(
            children: [
              Container(
                padding: EdgeInsets.only(
                  top: 50.0,
                ),
                child: CustomScrollView(
                  slivers: [
                    SliverToBoxAdapter(
                      child: Column(
                        children: [
                          Text('Testing 123; expanded: ${expanded}',
                              style: Theme.of(context).textTheme.labelLarge),
                          TextButton(
                              onPressed: () {
                                print("Pressed...");
                                setState(() {
                                  if (!this.expanded) {
                                    this._controller.animateTo(0.5,
                                        duration: Durations.short4,
                                        curve: Curves.easeIn);
                                  }
                                  this.expanded = !expanded;
                                });
                              },
                              child: Text('Toggle'))
                        ],
                      ),
                    ),
                  ],
                ),
              ),
              SingleChildScrollView(
                physics: const ClampingScrollPhysics(),
                controller: scrollController,
                child: Column(
                  children: [
                    SizedBox(
                      height: 12.0,
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Container(
                          width: 30,
                          height: 5,
                          decoration: BoxDecoration(
                              color: Colors.grey[300],
                              borderRadius:
                                  BorderRadius.all(Radius.circular(12.0))),
                        ),
                      ],
                    ),
                    SizedBox(
                      height: 18.0,
                    ),
                  ],
                ),
              ),
            ],
          );
        },
      ),
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.19.5, on macOS 12.7.4 21H1123 darwin-x64, locale en-GB)
    • Flutter version 3.19.5 on channel stable at /Users/alex/Programming/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 300451adae (3 weeks ago), 2024-03-27 21:54:07 -0500
    • Engine revision e76c956498
    • Dart version 3.3.3
    • DevTools version 2.31.1

[✗] Android toolchain - develop for Android devices
    ✗ Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.


[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14C18
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions).

[✓] VS Code (version 1.88.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.86.0

[✓] Connected device (3 available)
    • iPhone Xs (mobile) • B70C7918-2640-4093-A07A-13CA1DC8E970 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-16-2 (simulator)
    • macOS (desktop)    • macos                                • darwin-x64     • macOS 12.7.4 21H1123 darwin-x64
    • Chrome (web)       • chrome                               • web-javascript • Google Chrome 123.0.6312.123

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 2 categories.
@huycozy huycozy added the in triage Presently being triaged by the triage team label Apr 15, 2024
@huycozy
Copy link
Member

huycozy commented Apr 15, 2024

Thanks for the report. Reproduced the issue on Flutter master channel as well.

flutter doctor -v (stable and master)
[✓] Flutter (Channel stable, 3.19.5, on macOS 14.1 23B74 darwin-x64, locale en-VN)
    • Flutter version 3.19.5 on channel stable at /Users/huynq/Documents/GitHub/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 300451adae (8 days ago), 2024-03-27 21:54:07 -0500
    • Engine revision e76c956498
    • Dart version 3.3.3
    • DevTools version 2.31.1

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/huynq/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME = /Users/huynq/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15C500b
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • android-studio-dir = /Applications/Android Studio.app/
    • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)

[✓] VS Code (version 1.87.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.84.0

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-x64     • macOS 14.1 23B74 darwin-x64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 123.0.6312.106

[✓] Network resources
    • All expected network resources are available.

• No issues found!
[!] Flutter (Channel master, 3.22.0-10.0.pre, on macOS 14.1 23B74 darwin-x64, locale en-VN)
    • Flutter version 3.22.0-10.0.pre on channel master at /Users/huynq/Documents/GitHub/flutter_master
    ! Warning: `flutter` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path.
    ! Warning: `dart` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path.
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 1002ce4b03 (12 hours ago), 2024-04-14 20:29:43 +0530
    • Engine revision 1a13c7d1f4
    • Dart version 3.5.0 (build 3.5.0-47.0.dev)
    • DevTools version 2.34.1
    • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/huynq/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME = /Users/huynq/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.3)
    • Xcode at /Applications/Xcode15.3.app/Contents/Developer
    • Build 15E204a
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • android-studio-dir = /Applications/Android Studio.app/
    • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)

[✓] VS Code (version 1.88.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.86.0

[✓] Connected device (3 available)
    • RMX2001 (mobile) • EUYTFEUSQSRGDA6D • android-arm64  • Android 11 (API 30)
    • macOS (desktop)  • macos            • darwin-x64     • macOS 14.1 23B74 darwin-x64
    • Chrome (web)     • chrome           • web-javascript • Google Chrome 123.0.6312.123

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

@huycozy huycozy added framework flutter/packages/flutter repository. See also f: labels. a: error message Error messages from the Flutter framework has reproducible steps The issue has been confirmed reproducible and is ready to work on team-framework Owned by Framework team found in release: 3.19 Found to occur in 3.19 found in release: 3.22 Found to occur in 3.22 and removed in triage Presently being triaged by the triage team labels Apr 15, 2024
@goderbauer goderbauer added P2 Important issues not at the top of the work list triaged-framework Triaged by Framework team labels Apr 16, 2024
@JaffaKetchup
Copy link

I have a very similar issue.

On debug, it is not a massive issue, it causes a slight weird behaviour, but normal working is quickly restored on the next scroll event. However, on release, it causes a massive issue, as the controller appears to stop sending events properly to somewhere where they are required.

Unlike this issue, the FAB remains stuck to where it is supposed to be. It's other widgets that rely on the controller for positioning that break.

Additionally, it appears to occur when scrolling quickly (maybe there's frames being skipped which causes the issue?), and occurs when the sheet meets its initialChildSize/snapSizes (which are the same for me).

Screenrecorder-2024-05-21-22-17-13-377.mp4
Error Log
The following assertion was thrown while dispatching notifications for DraggableScrollableController:
setState() or markNeedsBuild() called during build.
This AnimatedBuilder widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was: AnimatedBuilder
    listenable: Instance of 'DraggableScrollableController'
    state: _AnimatedState#74c10
The widget which was currently being built when the offending call was made was: DraggableScrollableSheet
    dirty
    dependencies: [_InheritedResetNotifier, _InheritedTheme, _LocalizationsScope-[GlobalKey#89bf2]]
    state: _DraggableScrollableSheetState#5bbc7

When the exception was thrown, this was the stack:
#0      Element.markNeedsBuild.<anonymous closure> (package:flutter/src/widgets/framework.dart:5047:9)
framework.dart:5047
#1      Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:5059:6)
framework.dart:5059
#2      State.setState (package:flutter/src/widgets/framework.dart:1223:15)
framework.dart:1223
#3      _AnimatedState._handleChange (package:flutter/src/widgets/transitions.dart:129:5)
transitions.dart:129
#4      ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:433:24)
change_notifier.dart:433
#5      ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:433:24)
change_notifier.dart:433
#6      ValueNotifier.value= (package:flutter/src/foundation/change_notifier.dart:555:5)
change_notifier.dart:555
#7      _DraggableSheetExtent.updateSize (package:flutter/src/widgets/draggable_scrollable_sheet.dart:583:18)
draggable_scrollable_sheet.dart:583
#8      _DraggableScrollableSheetScrollController.reset (package:flutter/src/widgets/draggable_scrollable_sheet.dart:838:12)
draggable_scrollable_sheet.dart:838

Happy to add some code, but unlikely to be able to have enough time to produce an MRE any time soon, as it's decently complex internally - and I'm pretty sure it's unrelated to what I'm doing, and instead an issue with the DSS looking at the log.

Is this a seperate issue? Or a different manifestation of the same one? Happy to file a new report if necessary. But would like this looked into!

@JaffaKetchup
Copy link

JaffaKetchup commented May 22, 2024

The issue at #146736 (comment) occurs regardless as to whether there is a FAB. The error does have the same callstack.

But, I also stick something to the position of the sheet (as visible in the video) by using AnimatedBuilder around the DraggableScrollableController to listen to it, so maybe it's this pattern that causes the issue (as I haven't checked, but maybe the FAB does something similar). Indeed, the error does fix itself if I remove the widget that does this.

@JaffaKetchup
Copy link

Here's an MRE that proves the issue occurs regardless of whether a FloatingActionButton is specified: #148829 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a: error message Error messages from the Flutter framework found in release: 3.19 Found to occur in 3.19 found in release: 3.22 Found to occur in 3.22 framework flutter/packages/flutter repository. See also f: labels. has reproducible steps The issue has been confirmed reproducible and is ready to work on P2 Important issues not at the top of the work list team-framework Owned by Framework team triaged-framework Triaged by Framework team
Projects
None yet
4 participants