flutter Hooks Widget useState() with Object

Viewed 1483

I am using useState to trigger refresh, now this is my define, I define a parameter with an object:

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

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

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

then pass it by parameter:

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,
                              ),
                            )
                          ],
                        ));

when I change the parameter object in onRefresh function:

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

but the subcomponent not triggered refresh, why did this happen? what should I to to trigger ArticlesPage rerender? this is my useState:

final counter = useState<ArticleRequest>();

what I have tried,make the state object editable

var counter = useState<ArticleRequest>();

but it still not work, did not trigger refresh.

1 Answers

I believe is because is the same instance of the object, the value notifier thinks the object did not change. Try to override hashCode and == or I'd suggest to use equatable package which helps comparing objects in dart.

Related