initState method
Called when this object is inserted into the tree.
The framework will call this method exactly once for each State object it creates.
Override this method to perform initialization that depends on the location at which this object was inserted into the tree (i.e., context) or on the widget used to configure this object (i.e., widget).
If a State's build method depends on an object that can itself change state, for example a ChangeNotifier or Stream, or some other object to which one can subscribe to receive notifications, then be sure to subscribe and unsubscribe properly in initState, didUpdateWidget, and dispose:
- In initState, subscribe to the object.
- In didUpdateWidget unsubscribe from the old object and subscribe to the new one if the updated widget configuration requires replacing the object.
- In dispose, unsubscribe from the object.
You should not use BuildContext.dependOnInheritedWidgetOfExactType from this method. However, didChangeDependencies will be called immediately following this method, and BuildContext.dependOnInheritedWidgetOfExactType can be used there.
Implementations of this method should start with a call to the inherited
method, as in super.initState()
.
Implementation
@override
void initState() {
super.initState();
ref.listenManual(
pagingControllerProvider(_pagingBuilderController.getProviderKey()),
(pre, next) {
if (pre != next) {
// 如果下拉刷新时正在请求下一页,则需要丢弃下一页的标识
// If a pull-to-refresh action occurs while the next page is being requested, the next page identifier should be discarded.
if ((next.isRefreshing ?? false) && _hasRequestedNextPage) {
needDropNextPageRequest = true;
lastIsRefresh = true;
if (next.needRequestData ?? false) {
initData();
}
} else {
_hasRequestedNextPage = false;
needDropNextPageRequest = false;
// 如果由下拉刷新变成了onGoing状态,如果列表数据项没有变化,且在列表中显示的最后一个数据项的index大于等于列表item数-invisibleItemsThreshold,则重新请求下一页数据
// If a pull-to-refresh action changes to the onGoing state, and if there are no changes in the list items while the index of the last visible item in the list is greater than or equal to the total item count minus the invisibleItemsThreshold, then request the next page of data again.
if (lastIsRefresh &&
next.status == PagingStatus.ongoing &&
pre?.itemList?.length == next.itemList?.length &&
lastBuildIndex >= (next.itemList?.length ?? 0) - widget.pagingDataController.invisibleItemsThreshold) {
lastIsRefresh = false;
requestNextPageData();
}
}
}
},
fireImmediately: false,
);
WidgetsBinding.instance.addPostFrameCallback((_) {
initData(isPageInit: true);
});
}