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

232

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);
Share:
232
Admin
Author by

Admin

Updated on January 02, 2023

Comments

  • Admin
    Admin over 1 year

    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'));
    
    • Hamza Mogni
      Hamza Mogni over 2 years
      So you are getting data in a Future<List<Adds>> and you want to append that to a List<Adds> ?
    • Admin
      Admin over 2 years
      @HamzaMogni I want append data to Future<List<Ads>> not to a List<Ads>
    • Alan Bosco
      Alan Bosco over 2 years
      stackoverflow.com/a/70358518/13912777 Hope this help your issue Thank you
    • Hamza Mogni
      Hamza Mogni over 2 years
      I don't think you can render a Future, that's why we use a FutureBuilder. Well you will have to convert your API response to a List<Ads>, and then append that to your currently shown List.
    • Admin
      Admin over 2 years
      @HamzaMogni can I append a normal list into a futurebuilder ?
    • Hamza Mogni
      Hamza Mogni over 2 years
      @IrushiNathashaKuruppu yes, just await for the response, convert it to a dart object and append it to your initial list (the one futurebuilder is listening to)
    • Admin
      Admin over 2 years
      @HamzaMogni Thankyou So much
  • Admin
    Admin over 2 years
    this function worked.. thankyou