How to append data into a Future<List<Adds>>

Issue

I want to create a on scroll data load. currently I am getting data(ad list) from a api call as a Future<List<Ads>>

in scroll listener function I just want to append next page data list.

there was a example of doing it with a list, I just want to do the same with a Future list

items.addAll(List.generate(42, (index) => 'Inserted $index'));

Solution

You could simply do this:

Future<List<Ads>> appendElements(Future<List<Ads>> listFuture, List<Ads> elementsToAdd) async {
  final list = await listFuture;
  list.addAll(elementsToAdd);
  return list;
}

And then call it like this:

appendedListFuture = appendElements(items, yourItemsToAdd);

Answered By – Stefan Galler

Answer Checked By – Marilyn (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.