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...).
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...).
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();
For ListView
there is a flag called reverse
which is false by default. Change it to true
In ListView has an attitude is reverse, set it is true to reverse your list:
ListView.builder(
reverse: true,
);
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(),
),)
Instead of reversing listview
, get your list reversed using reversed property or reverse function.
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,
);
},
),
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)
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,
),
`
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())
]
])
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);
}
List<int> numbers = [1, 2, 3, 4, 5, 6, 7];
List<int> reversedNumbers = numbers.reversed.toList();
SingleChildScrollView(
reverse: false,
child: ListView.builder(
shrinkWrap: true,
reverse: true,
physics: NeverScrollableScrollPhysics(),
itemCount: 10,
itemBuilder: (context, index){
return Container();
},
),
)
© 2022 - 2024 — McMap. All rights reserved.