简体   繁体   English

Flutter bloc - 铸造问题

[英]Flutter bloc - Casting issue

I have an issue when trying to update a bloc's state locally.尝试在本地更新集团的 state 时遇到问题。 I am trying to retrieve the state to manipulate the data and then emit a new state so it reflects those changes in the UI.我正在尝试检索 state 来操作数据,然后发出一个新的 state 以便它反映 UI 中的这些更改。 I am not trying to manipulate the state itself just the property.我不是想操纵 state 本身,只是财产。 The property is of type List this class is defined like this该属性是列表类型,此 class 定义如下

This entity is in the domain layer.该实体位于域层中。 I have another class at the data layer which is defined like this我在数据层有另一个 class 定义如下

I am trying to modify one user's email and name but when retrieving the property is being parsed to List which is not correct since my porperty is defined as a List and when trying to update the list it fails.我正在尝试修改一个用户的 email 和名称,但是当检索该属性时,它被解析为不正确的 List,因为我的属性被定义为 List,并且在尝试更新列表时失败。 I do not want to cast the entire list to a userDataModel since that layer(presentation) where the bloc is should not know about models just entities.我不想将整个列表转换为 userDataModel,因为该块所在的层(表示)不应该只知道模型的实体。

I have tried to specified the types in both classes but I still get the same error.我试图在两个类中指定类型,但我仍然得到同样的错误。 I have tried to copyWith the class and still having the same result.我尝试使用 class 进行复制,但结果仍然相同。 I have tried casting the list from List to List but does take effect.我尝试将列表从列表转换为列表,但确实生效。

Here is the error I am getting in the console.这是我在控制台中遇到的错误。

Maybe I am doing things wrong, I have never had to modified a state locally like this.也许我做错了,我从来没有像这样在本地修改过 state。 Any advice with an example would be really helpful on how to handle this scenario.任何带有示例的建议对于如何处理这种情况都非常有帮助。

在此处输入图像描述

在此处输入图像描述

Ok, after a good night sleep I found that this is the best way to update a bloc.好的,经过一夜好眠后,我发现这是更新集团的最佳方式。

//!Modify user in users list
final UsersListLoaded actualState = state as UsersListLoaded;
List<UserData> modifiedList = actualState.usersList.map((userData) {
  return userData.id == event.user.id ? event.user : userData;
}).toList();

And afterwards emit the loaded state with proper data.然后用正确的数据发出加载的 state。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM