【问题标题】:Flutter BLoC `buildWhen` propertyFlutter BLoC `buildWhen` 属性
【发布时间】:2021-05-17 01:41:36
【问题描述】:

我在这段代码的最后一行收到_CastError 错误

BlocBuilder buildUsernameField() {
  return BlocBuilder<ProfileBloc, ProfileState>(
    buildWhen: (previous, current) => previous != current && current is EditingUserInfo,
    builder: (context, state) => TextField(
      keyboardType: TextInputType.name,
      controller: TextEditingController(
          text: (state as EditingUserInfo).username.value),

这么说

I/flutter (26787): The following _CastError was thrown building BlocBuilder<ProfileBloc, ProfileState>(dirty, state:
I/flutter (26787): _BlocBuilderBaseState<ProfileBloc, ProfileState>#25b87):
I/flutter (26787): type 'Success' is not a subtype of type 'EditingUserInfo' in type cast

所以发生的事情是,当我处于另一个状态(成功)时,它会尝试构建该小部件。但是在buildWhen参数中,我指定了widget应该只在状态为EditingUserInfo时构建。

据我所知,这个错误不应该发生。

这是我的ProfileState

part of 'profile_bloc.dart';

abstract class ProfileState extends Equatable {
  const ProfileState();
  
  @override
  List<Object> get props => [];
}

class ProfileInitial extends ProfileState {}

class EditingUserInfo extends ProfileState {
  final Username username;
  final Bio bio;
  final PhotoUrl photoUrl;
  final City city;
  final FormzStatus status;

  const EditingUserInfo({
    this.username = const Username.pure(),
    this.bio = const Bio.pure(),
    this.photoUrl = const PhotoUrl.pure(),
    this.city = const City.pure(),
    this.status = FormzStatus.pure,
  });

  EditingUserInfo copyWith({Username username, Bio bio, PhotoUrl photoUrl, City city, FormzStatus status}){
    return EditingUserInfo(
      username: username ?? this.username,
      bio: bio ?? this.bio,
      photoUrl: photoUrl ?? this.photoUrl,
      city: city ?? this.city,
      status: status ?? this.status,
    );
  }

  @override
  List<Object> get props => [username, bio, photoUrl, city];
}

class Loading extends ProfileState {}

class Error extends ProfileState {
  final String message;

  const Error({this.message});

  @override
  List<Object> get props => [message];
}

class Success extends ProfileState {
  final String message;

  const Success({this.message});

  @override
  List<Object> get props => [message];
}

【问题讨论】:

  • 显示您的状态文件ProfileState 和扩展它的类。
  • 我用我的 ProfileState 类更新了帖子

标签: flutter dart bloc


【解决方案1】:

您仍然需要检查状态变量是否正确。每次更改时都会检查状态,因此状态变量仍然可以是不同的状态,除非 buildWhen 条件为真,否则它不会重建。

BlocBuilder buildUsernameField() {
return BlocBuilder<ProfileBloc, ProfileState>(
buildWhen: (previous, current) => previous != current && current is EditingUserInfo,
builder: (context, state) {
 if(state is EditingUserInfo) {
  TextField(
  keyboardType: TextInputType.name,
  controller: TextEditingController(
      text: state.username.info)
}
}

【讨论】:

  • 好吧,这意味着我不太了解buildWhenbuilder 字段。到目前为止,我认为builder 中返回的小部件将在且仅当buildWhen 中指定的布尔条件为真时自行构建。但你现在向我展示的是我认为是错误的。小部件不应返回builder,而是返回buildWhen,仅在条件为真时构建。是这样吗?
  • 不,我错了。你所拥有的大部分是正确的,但是在构建器方法中,你仍然需要检查以确保它是正确的状态。 BlocBuilder 仍然检查每个状态更改,这意味着“状态”变量仍然可以是不同的状态,除非它处于该状态,否则它不会重建。我已经编辑了我的答案。
  • 我明白了,但 buildWhen 字段的用途是什么?因为顾名思义,这个属性的全部意义在于检查构建器函数应该何时运行:O
  • 是的。这意味着构建器只会在您的条件为真时重建小部件,但它仍会“检查”所有状态更改。只需确保在所有其他条件下都返回 false。
猜你喜欢
  • 2021-08-02
  • 2020-08-06
  • 2021-03-19
  • 2020-06-15
  • 2020-04-18
  • 1970-01-01
  • 2021-03-15
  • 2020-05-06
  • 2020-09-28
相关资源
最近更新 更多