如何解决分页不成功

I am using pagination to load my response data. I saw the similar question here. I followed and tried the solution but now I have a problem. I can only get the data of first page and second page. I cannot load from page 3 till last page. Can I know why?

这是我的代码:

PagesData pagesDataFromJson(String str) => PagesData.fromJson(json.decode(str));
String pagesDataToJson(PagesData data) => json.encode(data.toJson());

class PagesData {
  int currentPage;
  List<Data> data;
  String firstPageUrl;
  int from;
  int lastPage;
  String lastPageUrl;
  String nextPageUrl;

  PagesData({
    this.currentPage,
    this.data,
    this.firstPageUrl,
    this.from,
    this.lastPage,
    this.lastPageUrl,
    this.nextPageUrl,
  });

  factory PagesData.fromJson(Map<String, dynamic> json) => PagesData(
    currentPage: json["current_page"],
    data: List<Data>.from(json["data"].map((x) => Data.fromJson(x))),
    firstPageUrl: json["first_page_url"],
    from: json["from"],
    lastPage: json["last_page"],
    lastPageUrl: json["last_page_url"],
    nextPageUrl: json["next_page_url"],
  );

  Map<String, dynamic> toJson() => {
    "current_page": currentPage,
    "data": List<dynamic>.from(data.map((x) => x.toJson())),
    "first_page_url": firstPageUrl,
    "from": from,
    "last_page": lastPage,
    "last_page_url": lastPageUrl,
    "next_page_url": nextPageUrl,
  };
}

class Data{
  int id;
  String image;

  Data({
    this.id,
    this.image,
  });

  factory Data.fromJson(Map<String, dynamic> json) => Data(
    id: json["id"],
    image: json["image"],
  );

  Map<String, dynamic> toJson() => {
    "id": id,
    "image": image,
  };
}

    class HomeScreen extends StatefulWidget {
      @override
      _State createState() => _State();
    }

    class _State extends State<HomeScreen > {
      GlobalKey<PaginatorState> paginatorGlobalKey = GlobalKey();
    @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Flutter Paginator'),
          ),
          body: Paginator.listView(
            key: paginatorGlobalKey,
            pageLoadFuture: sendPagesDataRequest,
            pageItemsGetter: listItemsGetterPages,
            listItemBuilder: listItemBuilder,
            loadingWidgetBuilder: loadingWidgetMaker,
            errorWidgetBuilder: errorWidgetMaker,
            emptyListWidgetBuilder: emptyListWidgetMaker,
            totalItemsGetter: totalPagesGetter,
            pageErrorChecker: pageErrorChecker,
            scrollPhysics: BouncingScrollPhysics(),
          ),
        );
      }

      Future<PagesData> sendPagesDataRequest(int page) async {
        try {
          String url = Uri.encodeFull("https://API_URL?page=$page");
          http.Response response = await http.get(url);

          PagesData pagesData = pagesDataFromJson(response.body);
          return pagesData;
        } catch (e) {
        }
      }

      List<dynamic> listItemsGetterPages(PagesDatapagesData) {
        List<Data> list = [];
        pagesData.data.forEach((value) {
          list.add(value);
        });
        return list;
      }

      Widget listItemBuilder(dynamic item, int index) {
        return Container(
          margin: const EdgeInsets.all(8),
          child: Column(
            children: <Widget>[
              new CachedNetworkImage(
                imageUrl: item.image,
              ),
            ],),
        );
      }

      Widget loadingWidgetMaker() {
        return Container(
          alignment: Alignment.center,
          height: 160.0,
        );
      }

      Widget errorWidgetMaker(PagesData countriesData, retryListener) {
        return Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.all(16.0),
              child: Text("error"),
            ),
            FlatButton(
              onPressed: retryListener,
              child: Text('Retry'),
            )
          ],
        );
      }

      Widget emptyListWidgetMaker(PagesData pageData) {
        return Center(
          child: Text('No data in the list'),
        );
      }

      int totalPagesGetter(PagesData pagesData) {
        return pagesData.lastPage;
      }

      bool pageErrorChecker(PagesData pagesData) {
        return false;
      }
    }

有人可以帮助我吗?提前致谢。

评论