【问题标题】:How to get value from an object which in the state (flutter_bloc)如何从处于状态(flutter_bloc)的对象中获取值
【发布时间】:2021-04-22 19:30:35
【问题描述】:

在构建器方法中,我达到了类似状态的值

return BlocBuilder<UsersOwnProfileBloc, UsersOwnProfileState>(
   cubit: widget.bloc,
   builder: (context, state) {
      if (state is FetchedUserSettingState) {
      bool account = state.userSettings.publicAccount
}

但我需要从 initState 中获取值。我需要设置小部件的值。我试过这样的东西,但我得到了错误

 @override
  void initState() {
    super.initState();
     UsersOwnProfileState state = BlocProvider.of<UsersOwnProfileBloc>(context).state;
     if (state is FetchedUserSettingState) {
       publicAccount = state.userSettings.publicAccount;
     }
} 

谁能告诉我如何在 initState 中获取状态值?

class UserSettingPage extends StatefulWidget {
  final UsersOwnProfileBloc bloc;

  const UserSettingPage({Key key, this.bloc}) : super(key: key);

  @override
  _UserSettingPageState createState() => _UserSettingPageState();
}

class _UserSettingPageState extends State<UserSettingPage> {
  bool newListingAlert;
  bool listingForSearchAlert;
  bool searchForListingAlert;
  bool followAlert;
  bool publicAccount;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      final state = BlocProvider.of<UsersOwnProfileBloc>(context).state;
      if (state is FetchedUserSettingState) {
        publicAccount = state.userSettings.publicAccount;
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<UsersOwnProfileBloc, UsersOwnProfileState>(
      cubit: widget.bloc,
      builder: (context, state) {
        if (state is FetchedUserSettingState) {
          return Scaffold(
            appBar: PreferredSize(
              preferredSize: Size.fromHeight(25.h),
              child: ListingEditAppBar(
                onCancel: () {
                  widget.bloc.add(FetchUserEvent(userId: CurrentUser.currentUser.id));
                  Navigator.pop(context);
                },
              ),
            ),
            body: Column(
              children: [
                PageTitle(title: "Preferences"),
                Expanded(
                  child: ListView(
                    children: [
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Text("Profilim herkese açık"),
                            Switch(
                              onChanged: (value) {
                                setState(() {
                                  publicAccount = value;
                                });
                              },
                              value: publicAccount,
                            )
                          ],
                        ),
                      )
                    ],
                  ),
                )
              ],
            ),
          );
        }
        return MyProgressIndicator();
      },
    );
  }
}

我已经添加了整个代码。我收到以下错误。

断言失败:布尔表达式不能为空 相关的导致错误的小部件是 切换

【问题讨论】:

    标签: flutter bloc flutter-bloc


    【解决方案1】:

    如果您想访问 initState 中的状态,您需要使用 WidgetsBinding 来访问它。但是,使用它可以确保构建您的小部件,然后触发获取值的方法。仅使用 BlocBuilder、Watch 或 Select 来获取您正在寻找的值会更快。

    但要回答您的问题,您可以执行以下操作

      WidgetsBinding.instance.addPostFrameCallback((_) {
        final state = BlocProvider.of<UsersOwnProfileBloc>(context).state;
        if (state is FetchedUserSettingState) {
          publicAccount = state.userSettings.publicAccount;
        }
      });
    

    【讨论】:

    • 我已按照您的建议更改了我的代码,但我收到“断言失败:布尔表达式不得为空”错误。我已经添加了整个代码。有什么建议吗?
    • 我为您提供的代码中没有任何布尔值,它必须在您的代码中的其他位置。尝试使用步进器调试您的代码,以查看其失败的位置以及 bool 为空的原因
    猜你喜欢
    • 1970-01-01
    • 2019-09-04
    • 1970-01-01
    • 2023-02-04
    • 2021-07-23
    • 1970-01-01
    • 2014-12-12
    • 2019-07-31
    • 2021-12-29
    相关资源
    最近更新 更多