jajpa / paging_library

A Flutter package for paginating a list view
MIT License
37 stars 7 forks source link

It doesn't reload after setState called. #7

Closed ana-sher closed 5 years ago

ana-sher commented 5 years ago

I need filters inside my page and when the filtered response came, I do the setState on data that should be reloaded and changed but nothing happened.

ana-sher commented 5 years ago

Also got problem because of you don't allow to change shrinkWrap property. Well, decided not to use this library.

pitazzo commented 5 years ago

Did you fond anyway to achieve this? I'm facing exactly the same situation.

ana-sher commented 5 years ago

Did you fond anyway to achieve this? I'm facing exactly the same situation.

I just decided to do pagination by myself. First you need to initialize your data in initState, and then it will be updated with method _getData. You also will need variables to identify loading and end of list:

  bool endOfList = false; 
  bool loading = true; // change it when you will load your data in initState

in build:

ListView.builder(
              scrollDirection: Axis.vertical,
              shrinkWrap: true,
              physics: AlwaysScrollableScrollPhysics(),
              itemBuilder: (context, position) {
                if (position < users.length) {
                  return UserCard(users[position]);
                } else if (position == users.length && !endOfList) {
                  pageData();
                  return _loadingCircle();
                } 
                return null;
              },
            ),

pageData method:

Future<List<User>> pageData() async {
    if (!loading) {
        await _getData((users.length / 10).round() + 1);
    }

    return users;
  }

get Data method:

_getData(int page) async {
    var filter = DataFilter(
        page: page, pageSize: 10, country: country, city: city);
    UserService.fetchData(filter).then((val) {
      setState(() {
        users.addAll(val);
        loading = false;
        if (val.length < 10) {
          endOfList = true;
        }
      });
    });
  }