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);
Author by
Admin
Updated on January 02, 2023Comments
-
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 over 2 yearsSo you are getting data in a
Future<List<Adds>>
and you want to append that to aList<Adds>
? -
Admin over 2 years@HamzaMogni I want append data to Future<List<Ads>> not to a List<Ads>
-
Alan Bosco over 2 yearsstackoverflow.com/a/70358518/13912777 Hope this help your issue Thank you
-
Hamza Mogni over 2 yearsI 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 over 2 years@HamzaMogni can I append a normal list into a futurebuilder ?
-
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 over 2 years@HamzaMogni Thankyou So much
-
-
Admin over 2 yearsthis function worked.. thankyou