Flutter List.generate But In Reverse?
I have a container that I am trying to print out items form a list, minus the last item in the list. I am using list.generate:
child: Container( width: double.infinity, color: Colors.white, child: ListView( padding: const EdgeInsets.only(top: 10.0), children: List.generate( Provider.of<WeekList>(context).listOfWeeks.toList().length - 1, (index) => ListTileCustom( index: index, ), ), ), ),
My problem is when it prints out my custom
ListTileCustom widget each new item added to the list is added to the bottom and not the top in the view.
In the picture above the order should be:
- Red Bar
- Red / Green Bar
- Green bar
and when I add new items by pressing the button they should be added to the top not the bottom.
I tried adding
reverse: true and that gets the order right but moves everything to the bottom and adds a ton of white space above them... Also not sure the scroll will work in the right direction at that point either.
To reverse a list in Dart, you can use
..reversed method. In the code you posted, that would be:
children: List.generate(/* omitted */).reversed.toList().
Another (probably better) solution is to use
for loop directly in
children, for example:
ListView( children: [ for (int i = 0; i < 5; i++) FlutterLogo(), ], )
With this approach you can easily invert the list (and omit one element, like you desired) by changing the loop, e.g.
for (int i = 10; i > 1; i--) or something.
- → How do you create a 12 or 24 mnemonics code for multiple cryptocurrencies (ETH, BTC and so on..)
- → Flutter: input text field don't work properly in a simple example..... where am I wrong?
- → Can I customize the code formatting of Dart code in Atom?
- → Is it possible to develop iOS apps with Flutter on a Linux virtual machine?
- → Display SnackBar in Flutter
- → JSON ObjectMapper in Flutter
- → Material flutter app source code
- → TabBarSelection No such method error
- → How do I set the animation color of a LinearProgressIndicator?
- → Add different routes/screens to Flutter app
- → Is there a way to get the size of an existing widget?
- → How to share a file using flutter
- → Is there an easy way to find particular text built from RichText in a Flutter test?