为什么在这种情况下更新了BLOC模型信息?

发布于 2025-01-31 18:48:10 字数 767 浏览 4 评论 0原文

创建一个以编辑用户配置文件的页面,我用存储在模型中的用户信息初始化一个变量,并使用该信息来编辑它:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

惜醉颜 2025-02-07 18:48:10

在计数您共享的代码中进行计数,此变量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 the intState by this other assignment UserModel userUpdate = new UserModel(....

I think it should be this.userUpdate = new UserModel(....

瞳孔里扚悲伤 2025-02-07 18:48:10

UserUpdate您在Initstate()内创建的创建在此外部无法调用,

如果要使用,请创建其他用户变量

late UserModel user;
late UserModel userUpdate;
UserModel defaultUser = UserModel(
      id: user.id,
      phones: user.phones
   );

@override
void initState() {
   user = BlocProvider.of<UserBloc>(context).user;
   super.initState();
}

userUpdate you created inside initState() cannot call outside this,

create other user variable if you want use

late UserModel user;
late UserModel userUpdate;
UserModel defaultUser = UserModel(
      id: user.id,
      phones: user.phones
   );

@override
void initState() {
   user = BlocProvider.of<UserBloc>(context).user;
   super.initState();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文