颤抖的钩子窗口小部件useState()与对象

时间:2020-10-22 05:49:24

标签: flutter

我正在使用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>();

但是它仍然不起作用,没有触发刷新。

1 个答案:

答案 0 :(得分:1)

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