How to reverse a list in Flutter

To reverse a list in Flutter, you can simply use a function in Dart called reversed. The syntax to reverse lists in Flutter is:

<List variable>.reversed.toList(); //returns the reversed list

Remember to add .toList() after reversed so that the function returns a List. Without .toList(), the function will return a Iterable. A simple example to reverse a list of integers is given below but you can use it with any List of predefined datatype.

List lst = new List(); 
lst = [1,2,3,4]; 
print(lst);   //prints [1,2,3,4] 
print(lst.reversed.toList()); //prints [4,3,2,1] 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.