首页 > 解决方案 > 颤振升级后:'!_debugLocked':不正确。- 未处理的异常:在构建期间调用 setState() 或 markNeedsBuild() - dev

问题描述

将颤振升级到 2.0.1(以及更高版本的 2.0.2)后,我在 iOS 和 android 调试时收到以下错误:“!navigator._debugLocked': is not true。”

在应用程序启动后,我运行我的landing_page.dart,它使用“ChangeNotifierProvider.value(value:AuthProvider.instance”调用“auth_provider”。

auth_provider 使用带有 pushReplacementNamed(_routeName) 的命令“NavigationService.instance.navigateToReplacement(“register”)”调用我的 registration_page.dart(如果 currentUser 未通过身份验证)。

错误:

Launching lib/main.dart on iPhone 12 Pro Max in debug mode...
lib/main.dart:1
Xcode build done.                                           48,4s
Connecting to VM Service at ws://127.0.0.1:57583/znc-m_yDVrI=/ws
flutter: completed
[VERBOSE-2:ui_dart_state.cc(186)] Unhandled Exception: setState() or markNeedsBuild() called during build.
This Overlay 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:
  Overlay-[LabeledGlobalKey<OverlayState>#fc67e]
The widget which was currently being built when the offending call was made was:
  LandingPage
#0      Element.markNeedsBuild.<anonymous closure>
package:flutter/…/widgets/framework.dart:4138
#1      Element.markNeedsBuild
package:flutter/…/widgets/framework.dart:4153
#2      State.setState (package:flutter/src/<…&gt;
[VERBOSE-2:ui_dart_state.cc(186)] Unhandled Exception: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 2997 pos 18: '!navigator._debugLocked': is not true.
#0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:46:39)
#1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:36:5)
#2      _RouteEntry.handlePush.<anonymous closure>
package:flutter/…/widgets/navigator.dart:2997
#3      TickerFuture.whenCompleteOrCancel.thunk
package:flutter/…/scheduler/ticker.dart:407

我的颤振医生:

[✓] Flutter (Channel stable, 2.0.2, on macOS 11.2.3 20D91 darwin-x64, locale de-DE)
    • Flutter version 2.0.2 at /Users/michael/Documents/Developer/flutter
    • Framework revision 8962f6dc68 (4 days ago), 2021-03-11 13:22:20 -0800
    • Engine revision 5d8bf811b3
    • Dart version 2.12.1

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at /Users/michael/Library/Android/sdk
    • Platform android-30, build-tools 30.0.2
    • 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 at /Applications/Xcode.app/Contents/Developer
    • Xcode 12.4, Build version 12D4e
    • CocoaPods version 1.10.1

[✗] Chrome - develop for the web (Cannot find Chrome executable at /Applications/Google Chrome.app/Contents/MacOS/Google
    Chrome)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.

[✓] Android Studio (version 4.0)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 49.0.2
    • Dart plugin version 193.7547
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

[✓] Connected device (1 available)
    • iPhone 12 Pro Max (mobile) • F2E4F560-8466-43F3-A33B-41039F6F4F8E • ios • com.apple.CoreSimulator.SimRuntime.iOS-14-4
      (simulator)

标签: iosflutternavigator

解决方案


我通过使用解决了这个问题

"WidgetsBinding.instance.addPostFrameCallback((_) {
      navigatorKey.currentState.popAndPushNamed(_routeName);"

代替 "navigatorKey.currentState.pushReplacementNamed(_routeName)"


推荐阅读