Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make a List with months from current to previous etc.. Flutter/Dart

i can think how can you make a list with months from current’octomber’ to previous etc etc, this list can have 12 months from now octomber 2022 to november 2021.
not hardcoded, automated.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use intl package like this:

List months = [];
for (var i = 0; i < 12; i++) {
  var monthNumber = (DateTime.now().month - i);
  var monthDate = DateFormat.M().parse(
      (monthNumber < 1 ? 12 - (-monthNumber) : monthNumber).toString());
  var month = DateFormat.MMMM().format(monthDate);
  months.add(month);
}
print("months = $months");//months = [October, September, August, July, June, May, April, March, February, January, December, November]

if you get your list in Int try this:

List months = [];
for (var i = 0; i < 12; i++) {
  var monthNumber = (DateTime.now().month - i);
  var month = (monthNumber < 1 ? 12 - (-monthNumber) : monthNumber);
  months.add(month);
}
print("months = $months");//months = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 12, 11]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading