为什么在这种情况下更新了BLOC模型信息?
创建一个以编辑用户配置文件的页面,我用存储在模型中的用户信息初始化一个变量,并使用该信息来编辑它:
late UserModel user;
late UserModel userUpdate;
@override
void initState() {
user = BlocProvider.of<UserBloc>(context).user;
UserModel userUpdate = new UserModel(
id: user.id,
phones: user.phones
);
super.initState();
}
我在删除我选择的手机的按钮旁边的列表中显示手机。 Onpressed按钮确实做到了这一点:
onPressed: () {
setState(() {
userUpdate.phones.remove(userUpdate.phones[index]);
});
},
在Appbar中有2个选项,返回而无需保存和保存。 如果我在不保存的情况下选择该选项,我只需执行navigator.pop(context);
这是我的问题,当从列表中删除手机时,我希望将其从Userupdate对象中删除,而在不保存的情况下返回时,没有任何事情。但是,如果我返回“编辑用户配置文件”页面,则该手机已从主模型中删除,这是我在用户变量中初始化的一个。为什么会发生这种情况?
摘要:我编辑一个从对象'1'创建的对象'2',更新对象'2'和对象'1'的值。
Creating a page to edit a user's profile I initialize a variable with the user's information stored in a model and create a user with that information to edit it:
late UserModel user;
late UserModel userUpdate;
@override
void initState() {
user = BlocProvider.of<UserBloc>(context).user;
UserModel userUpdate = new UserModel(
id: user.id,
phones: user.phones
);
super.initState();
}
I display the phones in a list next to a button that deletes the phone of my choice. The onpressed button does exactly this:
onPressed: () {
setState(() {
userUpdate.phones.remove(userUpdate.phones[index]);
});
},
In the appBar there are 2 options, return without saving and save.
If I choose the option without saving, I simply do a Navigator.pop(context);
Here is my problem, when deleting a phone from the list, I expect it to be removed from the userUpdate object and when returning without saving nothing happens. However, if I go back to the edit user profile page, that phone has been removed from the main model, the one I initialize in the user variable. Why does this happen?
Summary: I edit an object '2' created from object '1', update a value of object '2' and object '1' is updated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在计数您共享的代码中进行计数,此变量
late usermodel UserUpdate;
从未分配,因为此其他分配是在Intstate
中被遮盖=新的usermodel(... 。我认为应该是
this.userupdate = new usermodel(...
。Taking in count the code you've shared, this variable
late UserModel userUpdate;
is never assigned, because is being shadowed in theintState
by this other assignmentUserModel userUpdate = new UserModel(...
.I think it should be
this.userUpdate = new UserModel(...
.UserUpdate
您在Initstate()内创建的创建在此外部无法调用,如果要使用,请创建其他用户变量
userUpdate
you created inside initState() cannot call outside this,create other user variable if you want use