本文介绍了在生成过程中调用setState()或markNeedsBuild()。无法将此叠加微件标记为需要构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用由{AbsorbPointer}(具体地说是{GestureDetector}onPanEnd)调用的‘fling’函数的SetState()中的‘showGeneralDialog’编写动画对话框脚本。我附上了简单的代码,我试着用未来的异步等待来做,等等,但我可能没有得到它。

在我的代码中将‘Child:EasyDialog’定义为空函数。这是我的调试控制台和附带的代码

如果有人能指导我如何在setState期间完成动画(PageBuilder),我将非常高兴。

[38;5;248m════════ Exception caught by widgets library ═══════════════════════════════════[39;49m
[38;5;244mThe following assertion was thrown building Builder(dirty, dependencies: [_LocalizationsScope-[GlobalKey#3e9b6]]):[39;49m
setState() or markNeedsBuild() called during build.

[38;5;244mThis 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.[39;49m
[38;5;244mThe widget on which setState() or markNeedsBuild() was called was: Overlay-[LabeledGlobalKey<OverlayState>#2acd6][39;49m
    [38;5;244mstate: OverlayState#6a2d6(entries: [OverlayEntry#9eb98(opaque: true; maintainState: false), OverlayEntry#7538d(opaque: false; maintainState: true), OverlayEntry#dfb68(opaque: true; maintainState: false), OverlayEntry#12c39(opaque: false; maintainState: true), OverlayEntry#3ef90(opaque: true; maintainState: false), OverlayEntry#53522(opaque: false; maintainState: true), OverlayEntry#07377(opaque: false; maintainState: false), OverlayEntry#e5cbf(opaque: false; maintainState: true), OverlayEntry#f3375(opaque: false; maintainState: false), OverlayEntry#b6bdc(opaque: false; maintainState: true), OverlayEntry#34ca0(opaque: false; maintainState: false)])[39;49m
[38;5;244mThe widget which was currently being built when the offending call was made was: Builder[39;49m
    [38;5;244mdirty[39;49m
    [38;5;244mdependencies: [_LocalizationsScope-[GlobalKey#3e9b6]][39;49m
[38;5;244mThe relevant error-causing widget was[39;49m
[38;5;248mSlideTransition[39;49m
 libmain.dart
[38;5;244mWhen the exception was thrown, this was the stack[39;49m
[38;5;244m#0      Element.markNeedsBuild.<anonymous closure>[39;49m
 package:flutter/…/widgets/framework.dart
[38;5;244m#1      Element.markNeedsBuild[39;49m
 package:flutter/…/widgets/framework.dart
[38;5;244m#2      State.setState[39;49m
 package:flutter/…/widgets/framework.dart
[38;5;244m#3      OverlayState.rearrange[39;49m
 package:flutter/…/widgets/overlay.dart
[38;5;244m#4      NavigatorState._flushHistoryUpdates[39;49m
 package:flutter/…/widgets/navigator.dart
[38;5;244m...[39;49m
[38;5;248m════════════════════════════════════════════════════════════════════════════════[39;49m
E/flutter (30352): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 2976 pos 18: '!navigator._debugLocked': is not true.
[38;5;244mE/flutter (30352): #0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:46:39)[39;49m
[38;5;244mE/flutter (30352): #1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:36:5)[39;49m
[38;5;244mE/flutter (30352): #2      _RouteEntry.handlePush.<anonymous closure>[39;49m
 package:flutter/…/widgets/navigator.dart
[38;5;244mE/flutter (30352): #3      TickerFuture.whenCompleteOrCancel.thunk[39;49m
 package:flutter/…/scheduler/ticker.dart
[38;5;244mE/flutter (30352): #4      _rootRunUnary (dart:async/zone.dart:1194:47)[39;49m
[38;5;244mE/flutter (30352): #5      _CustomZone.runUnary (dart:async/zone.dart:1097:19)[39;49m
[38;5;244mE/flutter (30352): #6      _FutureListener.handleValue (dart:async/future_impl.dart:150:18)[39;49m
[38;5;244mE/flutter (30352): #7      Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:703:45)[39;49m
[38;5;244mE/flutter (30352): #8      Future._propagateToListeners (dart:async/future_impl.dart:732:32)[39;49m
[38;5;244mE/flutter (30352): #9      Future._completeWithValue (dart:async/future_impl.dart:536:5)[39;49m
[38;5;244mE/flutter (30352): #10     Future._asyncCompleteWithValue.<anonymous closure> (dart:async/future_impl.dart:574:7)[39;49m
[38;5;244mE/flutter (30352): #11     _rootRun (dart:async/zone.dart:1186:13)[39;49m
[38;5;244mE/flutter (30352): #12     _CustomZone.run (dart:async/zone.dart:1090:19)[39;49m
[38;5;244mE/flutter (30352): #13     _CustomZone.runGuarded (dart:async/zone.dart:994:7)[39;49m
[38;5;244mE/flutter (30352): #14     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1034:23)[39;49m
[38;5;244mE/flutter (30352): #15     _microtaskLoop (dart:async/schedule_microtask.dart:41:21)[39;49m
[38;5;244mE/flutter (30352): #16     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)[39;49m
E/flutter (30352):
E/flutter (30352): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 2976 pos 18: '!navigator._debugLocked': is not true.
[38;5;244mE/flutter (30352): #0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:46:39)[39;49m
[38;5;244mE/flutter (30352): #1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:36:5)[39;49m
[38;5;244mE/flutter (30352): #2      _RouteEntry.handlePush.<anonymous closure>[39;49m
 package:flutter/…/widgets/navigator.dart
[38;5;244mE/flutter (30352): #3      TickerFuture.whenCompleteOrCancel.thunk[39;49m
 package:flutter/…/scheduler/ticker.dart
[38;5;244mE/flutter (30352): #4      _rootRunUnary (dart:async/zone.dart:1194:47)[39;49m
[38;5;244mE/flutter (30352): #5      _CustomZone.runUnary (dart:async/zone.dart:1097:19)[39;49m
[38;5;244mE/flutter (30352): #6      _FutureListener.handleValue (dart:async/future_impl.dart:150:18)[39;49m
[38;5;244mE/flutter (30352): #7      Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:703:45)[39;49m
[38;5;244mE/flutter (30352): #8      Future._propagateToListeners (dart:async/future_impl.dart:732:32)[39;49m
[38;5;244mE/flutter (30352): #9      Future._completeWithValue (dart:async/future_impl.dart:536:5)[39;49m
[38;5;244mE/flutter (30352): #10     Future._asyncCompleteWithValue.<anonymous closure> (dart:async/future_impl.dart:574:7)[39;49m
[38;5;244mE/flutter (30352): #11     _rootRun (dart:async/zone.dart:1186:13)[39;49m
[38;5;244mE/flutter (30352): #12     _CustomZone.run (dart:async/zone.dart:1090:19)[39;49m
[38;5;244mE/flutter (30352): #13     _CustomZone.runGuarded (dart:async/zone.dart:994:7)[39;49m
[38;5;244mE/flutter (30352): #14     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1034:23)[39;49m
[38;5;244mE/flutter (30352): #15     _microtaskLoop (dart:async/schedule_microtask.dart:41:21)[39;49m
[38;5;244mE/flutter (30352): #16     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)[39;49m
E/flutter (30352):
I/ViewRootImpl@738e9ee[MainActivity](30352): ViewPostIme pointer 0
  fling(details) {
    final simulation = FrictionSimulation(/**/

    );
    _spinController.animateWith(simulation).then((_) => setState(() {
          absorbing = false;
          Timer(Duration(milliseconds: 500), () {
            showGeneralDialog(
              barrierLabel: "Label",
              barrierDismissible: true,
              barrierColor: Colors.black.withOpacity(0.5),
              transitionDuration: Duration(milliseconds: 700),
              context: context,
              pageBuilder: (context, anim1, anim2) {
                return Align(
                  alignment:
                      _fromTop ? Alignment.topCenter : Alignment.bottomCenter,
                  child: Container(
                    height: 300,
                    child: SizedBox.expand(
                        child: EasyDialog(
                      closeButton: true,
                      cardColor: Colors.white.withOpacity(0.3),
                      contentPadding: const EdgeInsets.only(bottom: 100),
                      title: Text(
                        "Task Dialog",
                        textScaleFactor: 1.2,
                      ),
                      description: Text(
                        text[Random().nextInt(text.length)],
                      ),
                      topImage: AssetImage('assets/images/cbg82.jpg'),
                      height: 400,
                      width: 350,
                    ).show(context)),
                    margin: EdgeInsets.only(
                        top: 50, left: 12, right: 12, bottom: 50),
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(40),
                    ),
                  ),
                );
              },
              transitionBuilder: (context, anim1, anim2, child) {
                return SlideTransition(
                  position: Tween(
                          begin: Offset(0, _fromTop ? -1 : 1),
                          end: Offset(0, 0))
                      .animate(anim1),
                  child: child,
                );
              },
            );
          });
        }));
    setState(() => absorbing = true);
  }
///

  @override
  Widget build(BuildContext context) {
    return Scaffold(
    /*appbars*/
      body: Container(
          child: Column(
            children: <Widget>[
              Card(
                  child: AbsorbPointer(
                    absorbing: absorbing,
                    child: GestureDetector(
                      behavior: HitTestBehavior.translucent,
                      onPanUpdate: (d) =>
                          _spinController.value += d.delta.dx / 100,
                      onPanEnd: fling,
                      child: SizedBox(
                        child: RotationTransition(
                            turns: _spinController,
                            child: FittedBox(
                                alignment: Alignment.center,
                                fit: BoxFit.fill,
                                child: Image.asset(
                                  _imageToShow,
                                  height: _aHeight,
                                  width: _aWidth,
                                ))),
                      ),
                    ),
                  )),
            ],
          )),
    );
  }

谢谢:)

推荐答案

您收到此错误是因为您在页面生成完成之前正在执行某些操作。因此,您必须等待。

使用addPostFrameCallback怎么样?

official flutter docs查看此信息。

您可以这样做:

  SchedulerBinding.instance.addPostFrameCallback((_) {
    //yourcode
  });
另一个原因可能是(在看不到完整代码的情况下很难判断)您在构建阶段立即执行onPressedonTap回调。确保您的代码中没有任何类似onTap: onTap()的内容。相反,您需要执行onTap: onTap

这篇关于在生成过程中调用setState()或markNeedsBuild()。无法将此叠加微件标记为需要构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 13:40