Skip to content

markNeedsBuild fails assertion with conditional Drawer #42580

Closed

Description

@nbros

I have a Drawer that is only enabled on the root page, using this code:

drawer: Navigator.of(context).canPop() ? null : Drawer(child: SettingsDrawer()),

When I modify a "dark mode" setting in the drawer that causes a rebuild, I get the following error:

════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown while finalizing the widget tree:
setState() or markNeedsBuild() called when widget tree was locked.

This Scaffold widget cannot be marked as needing to build because the framework is locked.
The widget on which setState() or markNeedsBuild() was called was: Scaffold
    dependencies: [_InheritedTheme, MediaQuery, _LocalizationsScope-[GlobalKey#a243d], Directionality]
    state: ScaffoldState#e99f6(tickers: tracking 2 tickers)
When the exception was thrown, this was the stack
#0      Element.markNeedsBuild.<anonymous closure> 
package:flutter/…/widgets/framework.dart:3692
#1      Element.markNeedsBuild 
package:flutter/…/widgets/framework.dart:3702
#2      State.setState 
package:flutter/…/widgets/framework.dart:1161
#3      ScaffoldState._drawerOpenedCallback 
package:flutter/…/material/scaffold.dart:1399
#4      DrawerControllerState.close 
package:flutter/…/material/drawer.dart:405
...
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by animation library ═════════════════════════════════
'package:flutter/src/widgets/framework.dart': Failed assertion: line 3658 pos 12: '_debugLifecycleState != _ElementLifecycle.defunct': is not true.
════════════════════════════════════════════════════════════════════════════════

In order to reproduce the issue, open the Drawer and tap the Dark Mode switch with the following (simplified) sample app:

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

void main() => runApp(AppConfig());

class Prefs extends ChangeNotifier {
  bool _darkMode = false;
  bool get darkMode => _darkMode;
  set darkMode(bool newValue) {
    if (newValue == _darkMode) return;
    _darkMode = newValue;
    notifyListeners();
  }
}

class AppConfig extends StatefulWidget {
  _AppConfigState createState() => _AppConfigState();
}

class _AppConfigState extends State<AppConfig> {
  final Prefs prefs = Prefs();

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<Prefs>.value(value: prefs, child: MyApp());
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final prefs = Provider.of<Prefs>(context);
    return MaterialApp(
      title: 'Test',
      theme: ThemeData(brightness: prefs.darkMode ? Brightness.dark : Brightness.light, primarySwatch: Colors.blue),
      home: Home(),
    );
  }
}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("test")),
      drawer: Navigator.of(context).canPop() ? null : Drawer(child: SettingsDrawer()),
      body: Container(),
    );
  }
}

class SettingsDrawer extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final prefs = Provider.of<Prefs>(context);
    return Scaffold(
      body: ListView(
        padding: EdgeInsets.zero,
        children: <Widget>[
          DrawerHeader(
            child: Text('Settings'),
            decoration: BoxDecoration(
              color: Colors.blue,
            ),
          ),
          ListTile(
            leading: Text("Dark Mode"),
            title: Switch(
              value: prefs.darkMode,
              onChanged: (value) => prefs.darkMode = value,
            ),
          ),
        ],
      ),
    );
  }
}

Activity

  1. added
    frameworkflutter/packages/flutter repository. See also f: labels.
    on Oct 12, 2019
  2. BondarenkoStas commented on Oct 12, 2019

    @BondarenkoStas

    @nbros please provide your flutter doctor -v output.

  3. nbros commented on Oct 12, 2019

    @nbros
    Author

    flutter doctor -v

    [√] Flutter (Channel stable, v1.9.1+hotfix.4, on Microsoft Windows [Version 10.0.18362.387], locale fr-FR)
        • Flutter version 1.9.1+hotfix.4 at C:\dev\flutter
        • Framework revision cc949a8e8b (2 weeks ago), 2019-09-27 15:04:59 -0700
        • Engine revision b863200c37
        • Dart version 2.5.0
    
     
    [√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
        • Android SDK at C:\dev\AndroidSDK
        • Android NDK location not configured (optional; useful for native profiling support)
        • Platform android-29, build-tools 29.0.2
        • ANDROID_HOME = C:\dev\AndroidSDK
        • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
        • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)
        • All Android licenses accepted.
    
    [!] Android Studio (version 3.5)
        • Android Studio at C:\Program Files\Android\Android Studio
        X Flutter plugin not installed; this adds Flutter specific functionality.
        X Dart plugin not installed; this adds Dart specific functionality.
        • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)
    
    [√] VS Code (version 1.39.1)
        • VS Code at C:\Users\nbros\AppData\Local\Programs\Microsoft VS Code
        • Flutter extension version 3.5.1
    
    [√] Connected device (1 available)
        • ONEPLUS A5000 • dc07fc83 • android-arm64 • Android 9 (API 28)
    
    ! Doctor found issues in 1 category.
    

    Please note that I also tested with master and I got the same error.

  4. TahaTesser commented on Apr 16, 2020

    @TahaTesser
    Contributor

    Issue still exist

    flutter doctor -v
    [✓] Flutter (Channel dev, v1.18.0-dev.5.0, on Mac OS X 10.15.4 19E287, locale
        en-GB)
        • Flutter version 1.18.0-dev.5.0 at /Users/taha/Code/flutter_dev
        • Framework revision 7f56b53de4 (4 days ago), 2020-04-12 12:00:01 -0400
        • Engine revision beb8a7ec48
        • Dart version 2.8.0 (build 2.8.0-dev.20.0 89b0f67261)
     
    [✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
        • Android SDK at /Users/taha/Code/sdk
        • Platform android-29, build-tools 29.0.3
        • ANDROID_HOME = /Users/taha/Code/sdk
        • Java binary at: /Applications/Android
          Studio.app/Contents/jre/jdk/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build
          1.8.0_212-release-1586-b4-5784211)
        • All Android licenses accepted.
    [✓] Xcode - develop for iOS and macOS (Xcode 11.4)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Xcode 11.4, Build version 11E146
        • CocoaPods version 1.9.1
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    [✓] Android Studio (version 3.6)
        • Android Studio at /Applications/Android Studio.app/Contents
        • Flutter plugin version 45.1.1
        • Dart plugin version 192.7761
        • Java version OpenJDK Runtime Environment (build
          1.8.0_212-release-1586-b4-5784211)
    [✓] VS Code (version 1.44.1)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.9.1
    [✓] Connected device (4 available)
        • SM M305F   • 32003c30dc19668f • android-arm64  • Android 10 (API 29)
        • macOS      • macOS            • darwin-x64     • Mac OS X 10.15.4 19E287
        • Chrome     • chrome           • web-javascript • Google Chrome
          81.0.4044.113
        • Web Server • web-server       • web-javascript • Flutter Tools
    • No issues found!
    
  5. darshankawar commented on Jan 27, 2021

    @darshankawar
    Member

    I verified this issue on latest stable and master and can see the assertion failing while switching to dark mode.

    error log
    ======== Exception caught by widgets library =======================================================
    The following assertion was thrown while finalizing the widget tree:
    setState() or markNeedsBuild() called when widget tree was locked.
    
    This Scaffold widget cannot be marked as needing to build because the framework is locked.
    The widget on which setState() or markNeedsBuild() was called was: Scaffold
      dependencies: [_LocalizationsScope-[GlobalKey#97888], Directionality, _InheritedTheme, _EffectiveTickerMode, MediaQuery]
      state: ScaffoldState#18c09(tickers: tracking 2 tickers)
    When the exception was thrown, this was the stack: 
    #0      Element.markNeedsBuild.<anonymous closure> (package:flutter/src/widgets/framework.dart:4297:9)
    #1      Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:4307:6)
    #2      State.setState (package:flutter/src/widgets/framework.dart:1264:14)
    #3      ScaffoldState._drawerOpenedCallback (package:flutter/src/material/scaffold.dart:1622:5)
    #4      DrawerControllerState.close (package:flutter/src/material/drawer.dart:472:28)
    ...
    ====================================================================================================
    
    ======== Exception caught by animation library =====================================================
    The following assertion was thrown while notifying listeners for AnimationController:
    'package:flutter/src/widgets/framework.dart': Failed assertion: line 4263 pos 12: '_debugLifecycleState != _ElementLifecycle.defunct': is not true.
    
    
    Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
    In either case, please report this assertion by filing a bug on GitHub:
      https://github.com/flutter/flutter/issues/new?template=BUG.md
    
    When the exception was thrown, this was the stack: 
    #2      Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:4263:12)
    #3      State.setState (package:flutter/src/widgets/framework.dart:1264:14)
    #4      DrawerControllerState._animationChanged (package:flutter/src/material/drawer.dart:336:5)
    #5      AnimationLocalListenersMixin.notifyListeners (package:flutter/src/animation/listener_helpers.dart:136:19)
    #6      AnimationController._tick (package:flutter/src/animation/animation_controller.dart:797:5)
    ...
    The AnimationController notifying listeners was: AnimationController#d79cc(⏮ 0.000; paused)
    ====================================================================================================
    W/libEGL  (27205): EGLNativeWindowType 0xd0cef008 disconnect failed
    W/libEGL  (27205): EGLNativeWindowType 0xd435a808 disconnect failed
    
    
    
    
    flutter doctor -v
    [✓] Flutter (Channel stable, 1.22.6, on Mac OS X 10.15.4 19E2269 darwin-x64,
        locale en-IN)
        • Flutter version 1.22.6 at /Users/dhs/documents/Fluttersdk/flutter
        • Framework revision 9b2d32b605 (5 days ago), 2021-01-22 14:36:39 -0800
        • Engine revision 2f0af37152
        • Dart version 2.10.5
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.0)
        • Android SDK at /Users/dhs/Library/Android/sdk
        • Platform android-30, build-tools 30.0.0
        • 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-6915495)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 12.3)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Xcode 12.3, Build version 12C33
        • CocoaPods version 1.9.3
    
    [!] Android Studio (version 4.1)
        • Android Studio at /Applications/Android Studio.app/Contents
        ✗ Flutter plugin not installed; this adds Flutter specific functionality.
        ✗ Dart plugin not installed; this adds Dart specific functionality.
        • Java version OpenJDK Runtime Environment (build
          1.8.0_242-release-1644-b3-6915495)
    
    [✓] VS Code (version 1.52.1)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.16.0
    
    [✓] Connected device (2 available)
        • SM A260G (mobile)      • 5200763ebcfa861f • android-arm • Android 8.1.0
          (API 27)
        • emulator-5554 (mobile) • emulator-5554    • android-x86 • Android 4.1.2
          (API 16) (emulator)
    
    ! Doctor found issues in 1 category.
    
    [✓] Flutter (Channel master, 1.26.0-13.0.pre.177, on Mac OS X 10.15.4 19E2269
        darwin-x64, locale en-IN)
        • Flutter version 1.26.0-13.0.pre.177 at
          /Users/dhs/documents/Fluttersdk/flutter
        • Framework revision 2eeeba9c09 (29 hours ago), 2021-01-25 20:52:30 -0800
        • Engine revision 6e391c5942
        • Dart version 2.12.0 (build 2.12.0-257.0.dev)
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.0)
        • Android SDK at /Users/dhs/Library/Android/sdk
        • Platform android-30, build-tools 30.0.0
        • 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-6915495)
        • All Android licenses accepted.
    
    [!] Xcode - develop for iOS and macOS
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Xcode 12.3, Build version 12C33
        ! CocoaPods 1.9.3 out of date (1.10.0 is recommended).
            CocoaPods is used to retrieve the iOS and macOS platform side's plugin
            code that responds to your plugin usage on the Dart side.
            Without CocoaPods, plugins will not work on iOS or macOS.
            For more info, see https://flutter.dev/platform-plugins
          To upgrade see
          https://guides.cocoapods.org/using/getting-started.html#installation for
          instructions.
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 4.1)
        • 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
        • Java version OpenJDK Runtime Environment (build
          1.8.0_242-release-1644-b3-6915495)
    
    [✓] VS Code (version 1.52.1)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.16.0
    
    [✓] Connected device (4 available)
        • SM A260G (mobile)      • 5200763ebcfa861f • android-arm    • Android 8.1.0
          (API 27)
        • emulator-5554 (mobile) • emulator-5554    • android-x86    • Android 4.1.2
          (API 16) (emulator)
        • macOS (desktop)        • macos            • darwin-x64     • Mac OS X
          10.15.4 19E2269 darwin-x64
        • Chrome (web)           • chrome           • web-javascript • Google Chrome
          88.0.4324.96
    
    ! Doctor found issues in 1 category.
    
    
  6. maheshj01 commented on Dec 8, 2022

    @maheshj01
    Member

    The issue is no longer reproducible on stable or the master channel.

    code sample
    import 'package:flutter/material.dart';
    import 'package:provider/provider.dart';
    
    void main() => runApp(AppConfig());
    
    class Prefs extends ChangeNotifier {
      bool _darkMode = false;
      bool get darkMode => _darkMode;
      set darkMode(bool newValue) {
        if (newValue == _darkMode) return;
        _darkMode = newValue;
        notifyListeners();
      }
    }
    
    class AppConfig extends StatefulWidget {
      _AppConfigState createState() => _AppConfigState();
    }
    
    class _AppConfigState extends State<AppConfig> {
      final Prefs prefs = Prefs();
    
      @override
      Widget build(BuildContext context) {
        return ChangeNotifierProvider<Prefs>.value(value: prefs, child: MyApp());
      }
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        final prefs = Provider.of<Prefs>(context);
        return MaterialApp(
          title: 'Test',
          theme: ThemeData(brightness: prefs.darkMode ? Brightness.dark : Brightness.light, primarySwatch: Colors.blue),
          home: Home(),
        );
      }
    }
    
    class Home extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: Text("test")),
          drawer: Navigator.of(context).canPop() ? null : Drawer(child: SettingsDrawer()),
          body: Container(),
        );
      }
    }
    
    class SettingsDrawer extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        final prefs = Provider.of<Prefs>(context);
        return Scaffold(
          body: ListView(
            padding: EdgeInsets.zero,
            children: <Widget>[
              const DrawerHeader(
                decoration: BoxDecoration(
                  color: Colors.blue,
                ),
                child: Text('Settings'),
              ),
              ListTile(
                leading: Text("Dark Mode"),
                title: Switch(
                  value: prefs.darkMode,
                  onChanged: (value) => prefs.darkMode = value,
                ),
              ),
            ],
          ),
        );
      }
    }
    flutter doctor -v (mac)
    [✓] Flutter (Channel master, 3.7.0-3.0.pre.33, on macOS 12.6 21G115 darwin-arm64, locale en-IN)
        • Flutter version 3.7.0-3.0.pre.33 on channel master 
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 5201856805 (38 minutes ago), 2022-12-05 18:27:21 -0800
        • Engine revision a309d239c4
        • Dart version 2.19.0 (build 2.19.0-463.0.dev)
        • DevTools version 2.20.0
        • 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 33.0.0-rc4)
        • Android SDK at /Users/mahesh/Library/Android/sdk
        • Platform android-33, build-tools 33.0.0-rc4
        • ANDROID_HOME = /Users/mahesh/Library/Android/sdk
        • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 14.0.1)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 14A400
        • CocoaPods version 1.11.3
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2021.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
        • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    
    [✓] IntelliJ IDEA Community Edition (version 2021.2.1)
        • IntelliJ at /Applications/IntelliJ IDEA CE.app
        • Flutter plugin version 61.2.4
        • Dart plugin version 212.5080.8
    
    [✓] VS Code (version 1.70.2)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.55.20221129
    
    [✓] Connected device (3 available)
        • iPhone 12 Pro (mobile) • 026D5789-9E78-4AD5-B1B2-3F8D4E7F65E4 • ios            •
          com.apple.CoreSimulator.SimRuntime.iOS-14-5 (simulator)
        • macOS (desktop)        • macos                                • darwin-arm64   • macOS 12.6 21G115 darwin-arm64
        • Chrome (web)           • chrome                               • web-javascript • Google Chrome 108.0.5359.94
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    • No issues found!
    
    [✓] Flutter (Channel stable, 3.3.9, on macOS 12.6 21G115 darwin-arm, locale en-IN)
        • Flutter version 3.3.9 on channel stable at /Users/mahesh/Development/flutter
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision b8f7f1f986 (24 hours ago), 2022-11-23 06:43:51 +0900
        • Engine revision 8f2221fbef
        • Dart version 2.18.5
        • DevTools version 2.15.0
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0-rc4)
        • Android SDK at /Users/mahesh/Library/Android/sdk
        • Platform android-33, build-tools 33.0.0-rc4
        • ANDROID_HOME = /Users/mahesh/Library/Android/sdk
        • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 14.0.1)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 14A400
        • CocoaPods version 1.11.3
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2021.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
        • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    
    [✓] IntelliJ IDEA Community Edition (version 2021.2.1)
        • IntelliJ at /Applications/IntelliJ IDEA CE.app
        • Flutter plugin version 61.2.4
        • Dart plugin version 212.5080.8
    
    [✓] VS Code (version 1.70.2)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.53.20221101
    
    [✓] Connected device (3 available)
        • iPhone 12 Pro (mobile) • 026D5789-9E78-4AD5-B1B2-3F8D4E7F65E4 • ios            •
          com.apple.CoreSimulator.SimRuntime.iOS-14-5 (simulator)
        • macOS (desktop)        • macos                                • darwin-arm64   • macOS 12.6 21G115 darwin-arm
        • Chrome (web)           • chrome                               • web-javascript • Google Chrome 107.0.5304.110
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    • No issues found!
    

    Closing as this seems to be fixed. Incase anyone still encounters the issue. Please feel free to write in the comments and we will reopen it.

    Thank you.

  7. added
    r: fixedIssue is closed as already fixed in a newer version
    on Dec 8, 2022
  8. github-actions commented on Mar 5, 2023

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  9. locked as resolved and limited conversation to collaborators on Mar 5, 2023
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

    c: crashStack traces logged to the consolefound in release: 1.22Found to occur in 1.22found in release: 1.26Found to occur in 1.26frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: material_uimaterial_ui package in flutter/packagesr: fixedIssue is closed as already fixed in a newer version

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions