flutter 拉动以刷新抖动中的 Package ,以便分页

6yoyoihd  于 2023-01-09  发布在  Flutter
关注(0)|答案(1)|浏览(124)

你好读者我是新的flutter,我想做分页,分页我使用一个包的名称是"拉到refersh".问题:我共有6后,每页的限制是3。1)当我到达列表的末尾,然后API将调用并设置当前页面变量值为2,它将加载第2页的所有数据作为一个新的列表,但我想合并第2页的数据到同一列表中...(分页,如Facebook Instagram等)。2)我的另一个问题是,当我拉refersh,页面是refersh完美,它会转到第一页,但问题是,当我再次转到列表末尾时,它显示我没有更多的数据(这意味着第2页API不工作),我有一个条件,如if else:-"hasNextPage",该变量从API获得,并且响应是1或2,如果响应是1,则在当前页之后有进一步的页,并且如果是0,则在当前页之后没有页。
我张贴我的代码和api链接也可以请你帮助我。
从API获取数据的方法

int currentPage = 1;
bool isRefersh = false;
final RefreshController refreshController = RefreshController();

Future<UserPost> getUserPost() async {

    var url =
        "LINK=$currentPage";
    var response = await http.get(Uri.parse(url));
    var jsondata = jsonDecode(response.body.toString());
    var _apiData = UserPost.fromJson(jsondata);

    if (response.statusCode == 200) {
      print("******getUserPost API");

      print("current page****$currentPage");
      print("hasnext page ${_apiData.hasNextPage}");

      print(jsondata);
      if(isRefersh == true){
        setState((){
          //currentPage = 1;
          isRefersh = false;
        });
        refreshController.refreshCompleted();
        return UserPost.fromJson(jsondata);
      }
      else{
        print("//////////////// has next page");
        print(_apiData.hasNextPage.toString());
        if(_apiData.hasNextPage == 0){
          refreshController.loadNoData();
          return UserPost.fromJson(jsondata);
        }else{
        }
        return UserPost.fromJson(jsondata);
      }

    } else {
      return UserPost.fromJson(jsondata);
    }
  }

拉入到引用的方法

onRefresh: () async{
                await Future.delayed(Duration(milliseconds: 1000));
                  setState(() {
                  isRefersh = true;
                  currentPage = 1;
                        });
             },

分页方法

onLoading: () async {
                if(snapshot.data!.hasNextPage == 0){
                 refreshController.loadNoData();
                 }else{
                  setState(() {
                  currentPage++;
                         });
                            await Future.delayed(Duration(milliseconds: 1000));
                            refreshController.loadComplete();
                          }

                        },
6yjfywim

6yjfywim1#

希望对你有帮助
试着这样做:

final RefreshController refreshController =
  RefreshController(initialRefresh: true);


 Future<bool> getPassengerData({bool isRefresh = false}) async {
    if (isRefresh) {
      currentPage = 1;
    } else {
      if (currentPage >= totalPages) {
        refreshController.loadNoData();
        return false;
      }
    }

    final Uri uri = Uri.parse(
        "api url=$currentPage&size=10");

    final response = await http.get(uri);

    if (response.statusCode == 200) {
      final result = passengersDataFromJson(response.body);

      if (isRefresh) {
        passengers = result.data;
      }else{
        passengers.addAll(result.data);
      }

      currentPage++;

      totalPages = result.totalPages;

      print(response.body);
      setState(() {});
      return true;
    } else {
      return false;
    }
  }

拉入到引用的方法

onRefresh: () async {
          final result = await getPassengerData(isRefresh: true);
          if (result) {
            refreshController.refreshCompleted();
          } else {
            refreshController.refreshFailed();
          }
        },

加载方法:

onLoading: () async {
          final result = await getPassengerData();
          if (result) {
            refreshController.loadComplete();
          } else {
            refreshController.loadFailed();
          }
        },

相关问题