简体   繁体   English

Flutter bloc:BlocBuilder 在第一次声明 state 时不会重新调用构建器方法

[英]Flutter bloc: BlocBuilder does not re-call builder method when stated state first time

I am trying to change the theme of my flutter app using bloc.我正在尝试使用 bloc 更改我的 flutter 应用程序的主题。 But I had a problem since the second time after the state was changed.但是自从第二次更改 state 后,我遇到了一个问题。

Sate is still updated, but the UI has not changed, the builder method has not run again状态还在更新,但是 UI 没有改变,builder 方法没有再次运行

在此处输入图像描述

My log:我的日志:

Performing hot restart...
Syncing files to device iPhone 11...
Restarted application in 1,386ms.
flutter: builder ThemeState
flutter: builder ThemeState
flutter: build
flutter: _ChangeThemeScreenState AppTheme.BlueDark
flutter: ThemeBloc AppTheme.BlueDark
flutter: ThemeBloc isThemeChange
flutter: builder ThemeState
flutter: _ChangeThemeScreenState AppTheme.BlueLight
flutter: ThemeBloc AppTheme.BlueLight
flutter: ThemeBloc isThemeChange
flutter: _ChangeThemeScreenState AppTheme.GreenDark
flutter: ThemeBloc AppTheme.GreenDark
flutter: ThemeBloc isThemeChange

my code:我的代码:

class ThemeBloc extends Bloc<ThemeEvent, ThemeState> {
  ThemeBloc() : super(ThemeState(themeData: appThemeData[AppTheme.GreenLight]));

  @override
  Stream<ThemeState> mapEventToState(
    ThemeEvent event,
  ) async* {
    print("ThemeBloc " + (event as ThemeChanged).theme.toString());
    if (event is ThemeChanged) {
      print("ThemeBloc " +'isThemeChange');
      yield ThemeState(themeData: appThemeData[event.theme]);
    }
  }
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => ThemeBloc(),
      child: BlocBuilder<ThemeBloc, ThemeState>(
        builder: (context, state) {
          print("builder " + state.toString());
          return MaterialApp(
              debugShowCheckedModeBanner: false,
              title: 'Bitradez',
              routes: routes,
              theme: state.themeData);
        }
      ),
    );
  }
}

when tab button when tab button

onTap: () {
                print("_ChangeThemeScreenState " + itemAppTheme.toString());
                BlocProvider.of<ThemeBloc>(context)
                    .add(ThemeChanged(theme: itemAppTheme));
              },

In your ThemeState class, try passing your themeData property to the props, I am pretty sure that I had the same problem as you a while ago.在您的ThemeState class 中,尝试将您的themeData属性传递给道具,我很确定我前一段时间遇到了同样的问题。 As far as I understand, the value that you set for the props is what bloc actually looks at when deciding whether to rebuild your widget or not.据我了解,您为props设置的值是bloc在决定是否重建您的小部件时实际查看的值。

I think something like this should work:我认为这样的事情应该有效:

List<Object> get props => [themeData]

You obviously still need yield this state correctly.你显然仍然需要正确地输出这个 state。

暂无
暂无

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

相关问题 Flutter bloc:更新状态不会重新调用 BlocBuilder - Flutter bloc: updating the state does not re-call BlocBuilder 颤振 + 块 6.0.6。 BlocBuilder 的“builder”回调与“buildWhen”回调的状态不同 - Flutter + Bloc 6.0.6. BlocBuilder's "builder" callback isn't provided with the same state as the "buildWhen" callback 使用 blocbuilder 检查 bloc state - Checking bloc state with blocbuilder 如果您在 BlocBuilder 中指定 bloc,flutter_bloc 会处理 cubit 吗? - Does flutter_bloc dispose of the cubit if you specify the bloc in the BlocBuilder? flutter_bloc BlocBuilder 没有重建,也没有在 BlocObserver 中观察到状态变化 - flutter_bloc BlocBuilder not rebuilding as well state change not observed in BlocObserver 何时在 BlocBuilder() 中提供一个块? - When to provide a bloc in a BlocBuilder()? flutter bloc:变量未在 BlocBuilder 中定义 - flutter bloc: variable is not defined in BlocBuilder 如果在 bloc builder 外部调用,flutter bloc 的 state 不会更新 - The state of flutter bloc is not updating if called outside the bloc builder Flutter:如果流是 Bloc 状态的一部分,如何将 StreamBuilder 与 BlocBuilder 一起使用? - Flutter: How to Use StreamBuilder With BlocBuilder in Case the Stream Is a Part of The Bloc's State? BlocBuilder 的 build 方法在从另一个 bloc 内部添加事件到这个 bloc 时不会被调用 - BlocBuilder's build method is not invoked when adding an event to this bloc from inside another bloc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM