简体   繁体   中英

Flutter bloc - Casting issue

I have an issue when trying to update a bloc's state locally. 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. I am not trying to manipulate the state itself just the property. The property is of type List this class is defined like this

This entity is in the domain layer. I have another class at the data layer which is defined like this

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. 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.

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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