Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reverse listview in Flutter

I implemented a listview which get data from a json.

I followed this implementation.

How could i reverse the order of this listview? (The first element should became the last, ecc...).

like image 523
Dev9977 Avatar asked Sep 28 '18 09:09

Dev9977


People also ask

What can I use instead of ListView in flutter?

ListView is a very important widget in a flutter. It is used to create the list of children But when we want to create a list recursively without writing code again and again then ListView. builder is used instead of ListView.

How do you reverse items in a list flutter?

We can reverse a List in Dart either by using built-in functions or using looping statements with swap mechanism. Reversing a List makes the first element to be placed at last position, second element placed at last but one position and so on.


2 Answers

You should be able to reverse the list before feeding it to the ListView.

List<String> animals = ['cat', 'dog', 'duck']; List<String> reversedAnimals = animals.reversed.toList(); 
like image 165
chemamolins Avatar answered Sep 20 '22 15:09

chemamolins


For ListView there is a flag called reverse which is false by default. Change it to true

like image 31
Dinesh Balasubramanian Avatar answered Sep 18 '22 15:09

Dinesh Balasubramanian