gpt4 book ai didi

flutter Hooks Widget useState() with Object

转载 作者:行者123 更新时间:2023-12-04 03:50:36 24 4
gpt4 key购买 nike

我正在使用 useState 来触发刷新,现在这是我的定义,我用一个对象定义了一个参数:

import '../../Item.dart';

class ArticleRequest{
ArticleRequest({
this.latestTime,
this.storiesType,
this.pageSize,
});

int latestTime;
StoriesType storiesType;
int pageSize=20;
}

然后通过参数传递:

return SmartRefresher(
onRefresh: () {
print("trigger");
articleRequest.latestTime = DateTime.now().millisecondsSinceEpoch;
counter.value = articleRequest;

_refreshController.refreshCompleted();
},
enablePullUp: true,
enablePullDown: true,
controller: _refreshController,
onLoading: () {
print("loading");
_refreshController.loadComplete();
},
footer: CustomFooter(
builder: (BuildContext context, LoadStatus mode) {
Widget body;
if (mode == LoadStatus.idle) {
body = Text("pull up load");
} else if (mode == LoadStatus.loading) {
//body = CupertinoActivityIndicator();
} else if (mode == LoadStatus.failed) {
body = Text("Load Failed!Click retry!");
} else if (mode == LoadStatus.canLoading) {
body = Text("release to load more");
} else {
body = Text("No more Data");
}
return Container(
height: 55.0,
child: Center(child: body),
);
},
),
child: CustomScrollView(
key: PageStorageKey(type),
slivers: <Widget>[
SliverOverlapInjector(
handle: NestedScrollView
.sliverOverlapAbsorberHandleFor(
context,
),
),
SliverPadding(
padding:
const EdgeInsets.symmetric(vertical: 8.0),
sliver: ArticlesPage(
type: counter.value,
),
)
],
));

当我更改 onRefresh 函数中的参数对象时:

articleRequest.latestTime = DateTime.now().millisecondsSinceEpoch;
counter.value = articleRequest;

但是子组件没有触发刷新,这是为什么?我应该如何触发 ArticlesPage 重新呈现?这是我的useState:

final counter = useState<ArticleRequest>();

我已经尝试过,使状态对象可编辑

var counter = useState<ArticleRequest>();

但是还是不行,没有触发刷新。

最佳答案

我相信是因为是对象的同一个实例,值通知器认为对象没有改变。尝试覆盖 hashCode== 或者我建议使用 equatable 包,它有助于比较 dart 中的对象。

关于flutter Hooks Widget useState() with Object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64476046/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com