简体   繁体   English

从BlocBuilder导航到新屏幕

[英]Navigate to a new screen from a BlocBuilder

I am trying to navigate to a new page, when my bloc is in a specific state. 当我的集团处于特定状态时,我试图导航到新页面。 However right now whenver I am trying to do this I get the following error: 但是现在,无论何时我尝试执行此操作,都会出现以下错误:

I/flutter (25673): Another exception was thrown: setState() or markNeedsBuild() called during build.

In the code down below I am doing some dispatches, here I am also checking if the current state of one of my blocs are something specific, then navigate to a new screen. 在下面的代码中,我正在执行一些调度,在这里,我还要检查我的一个区块的当前状态是否特定,然后导航到新屏幕。

if (state is Finished) {
      startOver(timerBloc, counterBloc, spotifyBloc, context);
      return [];
    }

My question is; 我的问题是; is there anyway to navigate to a new screen from a BlocBuilder? 无论如何,是否可以从BlocBuilder导航到新屏幕?

What i've tried: I have tried to give different contexts, passing it down, and even making it within its own class. 我尝试过的内容:我尝试过提供不同的上下文,将其传递下来,甚至在自己的类中创建。

All classes are stateless. 所有类都是无状态的。

The whole bloc can be seen in this pastebin . 整个集团可以在此pastebin中看到。

You should avoid placing any 'actions' within the build statement... I would use a StatefulWidget and place those actions in the initState(). 您应该避免在build语句中放置任何“动作” ...我将使用StatefulWidget并将这些动作放置在initState()中。 However, since you are using Flutter Bloc package(felangel.github.io/bloc), you can use BlocListener as an alternative. 但是,由于使用的是Flutter Bloc软件包(felangel.github.io/bloc),因此可以使用BlocListener作为替代。

Something like this: 像这样:

class Actions extends StatelessWidget {
AudioPlayer audioPlayer = new AudioPlayer();

@override
Widget build(BuildContext context) {

   final TimerBloc timerBloc = BlocProvider.of<TimerBloc>(context);
   return BlocListener<TimerBloc, TimerState>(
     listener: (context, state) {

       if (state is Finished) {
           startOver(timerBloc, counterBloc, spotifyBloc, context);
       }

     },
     child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: _mapStateToActionButtons(
          timerBloc: BlocProvider.of<TimerBloc>(context),
          counterBloc: BlocProvider.of<CounterBloc>(context),
          spotifyBloc: BlocProvider.of<SpotifyBloc>(context),
          adBloc: BlocProvider.of<AdBloc>(context),
        ),
   );

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM