我_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),
Run Code Online (Sandbox Code Playgroud)
这么说
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
Run Code Online (Sandbox Code Playgroud)
所以发生的事情是,当我处于另一种状态(成功)时,它会尝试构建该小部件。但在buildWhen参数中,我指定小部件仅应在状态为 时构建EditingUserInfo。
据我了解,这个错误不应该发生。
这是我的ProfileState:
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
Run Code Online (Sandbox Code Playgroud)
您仍然需要检查状态变量是否处于正确的状态。每次更改时都会检查状态,因此状态变量仍然可以是不同的状态,除非 buildWhen 条件为真,否则它不会重建。
BlocBuilder buildUsernameField() {
return BlocBuilder<ProfileBloc, ProfileState>(
buildWhen: (previous, current) => previous != current && current is EditingUserInfo,
builder: (context, state) {
if(state is EditingUserInfo) {
return TextField(
keyboardType: TextInputType.name,
controller: TextEditingController(
text: state.username.info)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13212 次 |
| 最近记录: |