1

我正在一起使用 redux 和 bloc。我有一个编辑配置文件屏幕,我从 redux 状态获取初始值,然后更改 bloc 中的值。但是当我的 bloc 状态发生变化时,redux 状态也会发生变化,而无需调度操作。我想在用户提交编辑值时更改 redux 状态。这是我构建方法的第一部分:

return StoreConnector<AppState, Store<AppState>>(
  converter: (store) => store,
  builder: (context, store) {
    return Scaffold(
      appBar: appBar,
      body: BlocProvider(
        create: (context) => EditUserProfileBloc(
          editEditUserProfileRepository: repository,
        )..add(
            EditUserProfileScreenLoaded(
              userProfile: store.state.userProfile,
            ),
          ),
        child: BlocListener<EditUserProfileBloc, EditUserProfileState>(
          listener: (context, state) async {},
          child: BlocBuilder<EditUserProfileBloc, EditUserProfileState>(
            builder: (context, state) {
              _bloc = BlocProvider.of<EditUserProfileBloc>(context);
                ...

和我的小部件来显示和更改图像:

ImageSelect(
    onSelect: (img) => _bloc.add(
        EditUserProfileImageChanged(imgSrc: img),
    ),
    child: Avatar(imgSrc: state.userProfile.avatar),
),

和我的集团:

if (event is EditUserProfileImageChanged) {
    UserProfile newUserProfile = state.userProfile;

    yield EditUserProfileBeforeTotalChanged();
    newUserProfile.avatar = event.imgSrc;
    yield EditUserProfileTotalState(userProfile: newUserProfile);
}

这里当我更改头像而不保存时,其他地方我使用头像也会更改。

4

0 回答 0