Ravinder Kumar
Ravinder Kumar

Reputation: 8000

Flutter: How to insert a list to another using setAll(0,list)

I am trying to insert data at 0 index of mPostList everything I receive from the API on Listview Scroll. This is my code,

List mPostList = List<PrivateChatMessageDataModel>();

var list = model.data.toList().reversed;
          mPostList.setAll(0,list);

But this code is clearning old data in mPostList and then inserting items in mPostList. So my question is,

How can I insert new data without losing previous data in list?


I have checked the official doc to understand setAll method, code written there is as below,

List<String> list = ['a', 'b', 'c'];
list.setAll(1, ['bee', 'sea']);
list.join(', '); // 'a, bee, sea'

As far as I understood it is removing data after 1th index. So is there is any other function to solve my question?

Upvotes: 1

Views: 1997

Answers (1)

Ravinder Kumar
Ravinder Kumar

Reputation: 8000

Answering question myself

I have solved this by insertAll,

  mPostList.insertAll(0,list);

What does insertAll do?

This increases the length of the list by the length of iterable and shifts all later objects towards the end of the list.

Read more here

Upvotes: 3

Related Questions