Flutter GETX -variable.value = newValue 和variable(newValue) 之间有什么区别?

发布于 2025-01-19 11:09:15 字数 565 浏览 4 评论 0原文

我一直在想,在flutter getx库中,如果我以这种方式定义了可观察的变量

Rxn<MyClass> myObsObject = Rxn<MyClass>();

,我可以通过以下方式更新其值:

myObsObject(newValue) / myObsObject(null)

或以等效的方式

myObsObject.value = newValue / myObsObject.value = null

今天,我发现这两种方法不是等效,因为在某些情况下(不知道如何重现ATM)在某些情况下采用第一种方式,但第二次使用getx widget的构建器,而第二个则没有。

那么上面两种方法之间的差异是什么?

I've always been thinking that in Flutter GetX library, if I have an observable variable defined in this way

Rxn<MyClass> myObsObject = Rxn<MyClass>();

I could update its value by doing:

myObsObject(newValue) / myObsObject(null)

or in an EQUIVALENT WAY

myObsObject.value = newValue / myObsObject.value = null

Today I discovered that the 2 methods are NOT equivalent, since doing the first way in some cases (don't know how to reproduce ATM) doesn't retrigger the builder of a GetX widget, while the second does.

So what is/are the difference(s) between the two methods up above?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

黄昏下泛黄的笔记 2025-01-26 11:09:15

你可以检查第一种方法的实现,你会发现它是这样的:

T call([T? v]) {
  if (v != null) {
    value = v;
  }
  return value;
}

所以基本上,它仍然对value进行赋值除非值为null

因此,myObsObject(newValue) 在所有情况下都应该可以正常工作,除非 newValuenull

You can check the implementation of the first way and you will see that it is this:

T call([T? v]) {
  if (v != null) {
    value = v;
  }
  return value;
}

So basically, it still does the assignment to value except when the value is null.

So myObsObject(newValue) should work fine in all cases except when newValue is null

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文