Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The getter 'iterator' was called on null. #8

Closed
heshesh2010 opened this issue Oct 20, 2020 · 1 comment
Closed

The getter 'iterator' was called on null. #8

heshesh2010 opened this issue Oct 20, 2020 · 1 comment

Comments

@heshesh2010
Copy link

Hi ,

i'm using mvc pattren here is snap of my code :

final PagingController<int, Restaurant> pagingNearRestaurantsController =
PagingController(firstPageKey: 0);

HomeController() {
pagingNearRestaurantsController.addPageRequestListener((pageKey) {
// _con.listenForOrders(pageKey);
// orderRepo.fetchPosts(orderRepo.nextUrl);
_fetchNearRestaurantsPage(pageKey);
});
}

Future _fetchNearRestaurantsPage(pageKey) async {

if(pageKey==0)
  newNearRestaurantsItems = await restaurantRepo.getNearRestaurants(deliveryAddress.value, deliveryAddress.value,null);
else
  newNearRestaurantsItems = await restaurantRepo.getNearRestaurants(deliveryAddress.value, deliveryAddress.value,restaurantRepo.nextUrl);

try{
  final isLastPage = newNearRestaurantsItems.length < _pageNearRestaurantsSize;
  if (isLastPage) {
    pagingNearRestaurantsController.appendLastPage(newNearRestaurantsItems);
  } else {
    final nextPageKey = pageKey + newNearRestaurantsItems.length;
    pagingNearRestaurantsController.appendPage(newNearRestaurantsItems, nextPageKey);
  }
} catch (error) {
  pagingNearRestaurantsController.error = error;
}

}

Future<List> getNearRestaurants(
Address myLocation, Address areaLocation,uri) async {

if(uri==null) {
uri = Helper.getUri('api/restaurants');
Map<String, dynamic> _queryParams = {};
SharedPreferences prefs = await SharedPreferences.getInstance();
Filter filter = Filter.fromJSON(
json.decode(
prefs.getString('filter') ?? new Filter("opened").toJson()));

if (!myLocation.isUnknown() && !areaLocation.isUnknown()) {
  _queryParams['myLon'] = myLocation.longitude.toString();
  _queryParams['myLat'] = myLocation.latitude.toString();
  _queryParams['areaLon'] = areaLocation.longitude.toString();
  _queryParams['areaLat'] = areaLocation.latitude.toString();
}
_queryParams['availability'] = filter.availability.toString();
uri = uri.replace(queryParameters: _queryParams);
uriFinal=uri;

}
else
uriFinal=uri;

http.Response response = await http.get(
    uriFinal );
var responseJson = json.decode(response.body);
var data = responseJson['data']['data'];

if (responseJson['data']['next_page_url'] != null) {
  nextUrl = responseJson['data']['next_page_url'];
}

return (data as List)
    .map((p) => Restaurant.fromJSON(p))
    .toList();

}

////
[VERBOSE-2:ui_dart_state.cc(177)] Unhandled Exception: NoSuchMethodError: The getter 'iterator' was called on null.
Receiver: null
Tried calling: iterator
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1 new List.from (dart:core-patch/array_patch.dart:57:19)
#2 PagingController.notifyStatusListeners (package:infinite_scroll_pagination/src/core/paging_controller.dart:134:28)
#3 PagingController.value= (package:infinite_scroll_pagination/src/core/paging_controller.dart:175:7)
#4 PagingController.error= (package:infinite_scroll_pagination/src/core/paging_controller.dart:59:5)
#5 HomeController._fetchNearRestaurantsPage (package:foodlobby_client_app/src/controllers/home_controller.dart:72:39)

#6 new HomeController. (package:foodlobby_client_app/src/controllers/home_controller.dart:40:7)
#7 PagingController.notifyPageRequestListeners. (package:infinite_scroll_pagination/src/core/paging<…

@EdsonBueno
Copy link
Owner

EdsonBueno commented Oct 20, 2020

Hi @heshesh2010.
Your problem is the same as the one of this issue.
Try using the same solution I provided there. If you face any problems implementing it, or if it doesn't work, please contact me on the chat and I'll be glad to help you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants