【问题标题】:Flutter show Snackbar inside buildFlutter 在构建中显示 Snackbar
【发布时间】:2020-05-29 00:48:28
【问题描述】:

我在我的应用中实现了 BLoC 模式,但遇到了问题。

一旦我有一个特殊的事件,我需要显示一个小吃店,但我有这个错误:

The following assertion was thrown building InteractWithMorceauPage(dirty, dependencies: [MediaQuery], state: _InteractWithMorceauPageState#25201):
setState() or markNeedsBuild() called during build.

This Scaffold 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: Scaffold
  dependencies: [TickerMode, MediaQuery, _InheritedTheme, _LocalizationsScope-[GlobalKey#dc380], Directionality]
  state: ScaffoldState#8a46b(tickers: tracking 3 tickers)
The widget which was currently being built when the offending call was made was: InteractWithMorceauPage
  dirty
  dependencies: [MediaQuery]
  state: _InteractWithMorceauPageState#25201

这就是我正在做的事情:

   else if (widget.state is TickNotPossibleState){
      buttonState = PAUSE;
      Scaffold.of(context).showSnackBar(SnackBar(content: Text("My Snackbar text!")));
    }

    return _generatePage(buttonState);

【问题讨论】:

  • 请分享更多代码。

标签: flutter dart flutter-bloc


【解决方案1】:

尝试在 bloc 监听器中使用 showsnackbar 并确保 bloc 监听器在脚手架下定义。

【讨论】:

  • 谢谢,这正是我要找的小部件!
猜你喜欢
  • 2017-03-27
  • 1970-01-01
  • 2020-09-24
  • 2019-12-15
  • 2019-08-28
  • 2019-05-24
  • 2020-04-23
  • 2019-07-27
  • 2022-01-06
相关资源
最近更新 更多