skip to Main Content

Hello Readers I am new in flutter and i want to do pagination, for pagination I am using one package which name is "pull to refersh".
Problems :
I have total 6 post and per page limits are 3.
1)When I reached at the end of list then api will call and set current page variable value is 2 and it will load all data of page 2 as a new list, but i want to merge page 2 data into same list… (Pagination like facebook instagram etc).
2)My another problem is when i pull for refersh, page is refersh perfectly and it will go to the first page but problem is, when i again go at the end of list it shows me no more data(which means page 2 api is not working)
I have one condition like if else:- "hasNextPage" this variable getting from api and the response is 1 or 2, if response is 1 then there further page after current page and if is 0 then there is no page after current page.

I am posting my code and api link also can you please help me.

Method for get data from 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);
    }
  }

Method for pull to Refersh

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

Method for Pagination

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

                        },

2

Answers


  1. I Hope it’s help you

    try this way :-

    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;
        }
      }
    

    Method for pull to Refersh

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

    Method for onLoading:

     onLoading: () async {
              final result = await getPassengerData();
              if (result) {
                refreshController.loadComplete();
              } else {
                refreshController.loadFailed();
              }
            },
    
    Login or Signup to reply.
  2. Try this way.

    when you get the response in second page just create new list with previous list.

    i.e var newData = […?dummyData.data, …?_apiData.data];

    than return this same list.

      UserPostModel dummyData = UserPostModel();
    
      Future<UserPostModel> getUserPost() async {
        var url =
            "*****?page=$currentPage";
        var response = await http.get(Uri.parse(url));
        var jsondata = jsonDecode(response.body.toString());
        var _apiData = UserPostModel.fromJson(jsondata);
    
        var newData = [...?dummyData.data, ...?_apiData.data];
    
        //totalPage = _apiData.totalPages as int?;
    
        if (response.statusCode == 200) {
          if (isRefersh == true) {
            setState(() {
              isRefersh = false;
            });
            refreshController.refreshCompleted();
          } else {
            if (_apiData.hasNextPage == 0) {
              refreshController.loadNoData();
            } else {
              refreshController.loadComplete();
            }
          }
          dummyData.data = newData;
    
          return dummyData;
        } else {
          return dummyData;
        }
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search