Reputation: 1281
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...).
Upvotes: 55
Views: 72282
Reputation: 101
You can reverse the order of your list before returning the ListView element.
ListView.separated(
itemCount: widget.orderList.length,
itemBuilder: (context, index) {
final reversed = widget.orderList.reversed.toList(); // reverse your list here
final item = reversed[index]; // assign it with index
return Text(
item.name, // the item name in my list
),
},
// The separators
separatorBuilder: (context, index) {
return const Divider(
color: Colors.grey,
);
},
),
Upvotes: 3
Reputation: 1
SingleChildScrollView(
reverse: false,
child: ListView.builder(
shrinkWrap: true,
reverse: true,
physics: NeverScrollableScrollPhysics(),
itemCount: 10,
itemBuilder: (context, index){
return Container();
},
),
)
Upvotes: 0
Reputation: 161
You can reverse it simply like it ==>>
ListView.builder(
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
var reverselist = snapshot.data!.reversed.toList();
and Then can be used as ==>
Text(
reverselist[index]
.tituloCategoria
.toString(),
overflow: TextOverflow.ellipsis,
),
`
Upvotes: 1
Reputation: 1364
List<int> numbers = [1, 2, 3, 4, 5, 6, 7];
List<int> reversedNumbers = numbers.reversed.toList();
Upvotes: 1
Reputation: 519
Here is a solution with null safety support of reverse the listview. Do not use reverse:true because it will reverse your data but when you open listview.builder widget in your app it will take automatically to scroll to the last item. To avoid that use this:-
ListView.builder(
itemCount: customer?.data.length ?? 0,
itemBuilder: (BuildContext context, int index) {
int itemCount = customer?.data.length ?? 0;
int reversedIndex = itemCount - 1 - index;
child: Container(
child: Text(value.allCustomerModel!.data![reversedIndex].attributes!.name.toString(),
),)
Upvotes: 8
Reputation: 1
List<int> number1 = List.empty(growable: true);
if (number1.isEmpty) {
number1.add(2);
number1.add(9);
number1.add(5);
number1.add(6);
print(number1);
number1.sort();
number1=number1.reversed.toList();
print(number1);
}
Upvotes: 0
Reputation: 1
Example :
List data = [
{
"number" : 1,
"iterator" : 0
},
{
"number" : 2,
"iterator" : 1
},
{
"number" : 3,
"iterator" : 2
},
];
**On ListView ... **
ListView(children: [
for (var i = data.length; i > 0; i--) ... [
Text(data[i]["number"].toString())
]
])
Upvotes: 0
Reputation: 1032
If you want to conditionally reverse the List that you are passing to the ListView, you can use this extension:
extension ListExtension<T> on List<T>{
List<T> reverse(bool condition){
return condition ? reversed.toList() : this;
}
}
and use it as [].reverse(yourCondition)
(dont forget to import this extension if you have defined it in some other file)
Upvotes: 0
Reputation: 2838
In ListView has an attitude is reverse, set it is true to reverse your list:
ListView.builder(
reverse: true,
);
Upvotes: 33
Reputation: 21748
For ListView
there is a flag called reverse
which is false by default. Change it to true
Upvotes: 39
Reputation: 20558
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();
Upvotes: 129
Reputation: 934
Instead of reversing listview
, get your list reversed using reversed property or reverse function.
Upvotes: 3