flutter - Flutter Bloc-按钮未触发状态更改

标签 flutter dart bloc

我有一个带有自己的main.dartmain_bloc.dartmain_event.dartmain_state.dart:

main_event:
- AppStarted
- GoingHome
- GoingTestHome

main_State:
- StateHome
- StateTestHome

main_bloc:
Stream<MainState> mapEventToState(
    MainEvent event,
  ) async* {
    if (event is AppStarted) {
      print('mainbloc: AppStarted');
dispatch(GoingHome());
    }
    if (event is GoingHome) {
      print('mainbloc: GoingHome');
      yield StateHome();
    }
    if (event is TestHome) {
      print('mainbloc: TestHome');
      yield StateTestHome();
    }
...
}

在我的main.dart中:
class _AppState extends State<App> {
  MainBloc _mainBloc;
  @override
  void initState() {
    super.initState();
    _mainBloc = MainBloc();
    _mainBloc.dispatch(AppStarted());/// <-- 1
  }
...

@override
  Widget build(BuildContext context) {
    return BlocProvider(
        builder: (_) => _mainBloc,
        child: MaterialApp(
          title: 'Home',
          theme: ThemeData(
            ...
          ),
          home: BlocListener<MainBloc, MainState>(
              bloc: _mainBloc,
              listener: (context, state) {
                print('state changed: ${state.toString()}');
                if (state is StateHome) {  /// <-- 2
                  print('main: StateHome');
                  _mainBloc.dispatch(TestHome()); /// <-- 2.1
                } else if (state is StateTestHome) {  /// <-- 3
                  print('main: StateTestHome');
                }
              },
              child: SplashPage()),
...

我做一个快速测试:
  • 当应用启动时,调用_mainBloc.dispatch(AppStarted()),它将从dispatch(GoingHome())调用main_bloc
  • BlocListener检测到状态更改(“StateHome”),因此:
    print('main: StateHome'); _mainBloc.dispatch(TestHome());
  • BlocListener再次检测到状态更改('StateTestHome'),因此:
    print('main: StateTestHome');

  • 在控制台中,我可以依次看到所有打印消息:
    Restarted application in 1,612ms.
    I/flutter (15369): mainbloc: AppStarted
    I/flutter (15369): mainbloc: GoingHome
    I/flutter (15369): state changed: StateHome
    I/flutter (15369): main: StateHome
    I/flutter (15369): mainbloc: TestHome
    I/flutter (15369): state changed: StateTestHome
    I/flutter (15369): main: StateTestHome
    

    到目前为止,它以我预期的方式运行。

    但是,当我取出代码2.1时,然后添加一个按钮来触发事件:
    ...
    floatingActionButton: FloatingActionButton(
            child: Text('test'),
            onPressed: () {
              _mainBloc.dispatch(TestHome());
            },
          ),
    ...
    

    我将获得这样的控制台:
    Restarted application in 2,738ms.
    I/flutter (15369): mainbloc: AppStarted
    I/flutter (15369): mainbloc: GoingHome
    I/flutter (15369): state changed: StateHome
    I/flutter (15369): main: StateHome
    I/flutter (15369):
    I/flutter (15369): mainbloc: TestHome ///<-- button trigger
    

    我预计:
    Restarted application in 2,738ms.
    I/flutter (15369): mainbloc: AppStarted
    I/flutter (15369): mainbloc: GoingHome
    I/flutter (15369): state changed: StateHome
    I/flutter (15369): main: StateHome
    I/flutter (15369):
    I/flutter (15369): mainbloc: TestHome ///<-- button trigger
    I/flutter (15369): state changed: StateTestHome ///<-- expectation
    I/flutter (15369): main: StateTestHome          ///<-- expectation
    

    似乎按钮交互未yield状态,或者BlocListener没有捕获状态更改事件。

    我在这里想念什么?我该如何解决?

    最佳答案

    Bloc捕获事件,但是您尝试发送连续的相同状态。在yield StateTestHome();上面尝试另一个产量

    喜欢

    yield StateIgnored(); // *****
    yield StateTestHome();
    

    编辑:或您可以只删除等同的抽象。

    关于flutter - Flutter Bloc-按钮未触发状态更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58266435/

    相关文章:

    firebase - flutter : Cannot display image from image url firestore

    dart - 使用 Barcode Scanner 中的值在 flutter 中添加新值后,列表不会扩展

    flutter - LocalStorage 流未完成?

    Flutter- GestureDetector 检测水平和垂直拖动的方向

    flutter - 在 android studio 上从 "Pub get"获取错误

    flutter - Flutter 中使用 auto_route 包实现同一页面的多个路径

    flutter - 使用flutter_bloc在flutter中提交时如何验证表单?

    ios - Flutter Firebase 回调不会在 iOS 13 上触发,但在 Android 上可以正常工作

    dart - 设计插件架构

    flutter - 从 BlocBuilder 导航到新屏幕