Flutter : Convert Integer To Month

Viewed 6217

I have Model and Dummy Data To testing like this :

Model


class TestingLoopingModel {
  String id;
  int month;
  int year;
  int value;

  TestingLoopingModel({
    this.id,
    this.month,
    this.year,
    this.value,
  });
}

Dummy Data


List<TestingLoopingModel> listTest = [
  //TODO Month 12 And 10 is not exist
  TestingLoopingModel(
    id: DateTime.now().toString(),
    month: 2,
    year: 2020,
    value: 2000,
  ),
  TestingLoopingModel(
    id: DateTime.now().toString(),
    month: 4,
    year: 2020,
    value: 1500,
  ),
  TestingLoopingModel(
    id: DateTime.now().toString(),
    month: 6,
    year: 2020,
    value: 3000,
  ),
  TestingLoopingModel(
    id: DateTime.now().toString(),
    month: 8,
    year: 2020,
    value: 3500,
  ),

  TestingLoopingModel(
    id: DateTime.now().toString(),
    month: 1,
    year: 2020,
    value: 5000,
  ),
  TestingLoopingModel(
    id: DateTime.now().toString(),
    month: 3,
    year: 2020,
    value: 4500,
  ),
  TestingLoopingModel(
    id: DateTime.now().toString(),
    month: 5,
    year: 2020,
    value: 2111,
  ),
  TestingLoopingModel(
    id: DateTime.now().toString(),
    month: 7,
    year: 2020,
    value: 5555,
  ),
  TestingLoopingModel(
    id: DateTime.now().toString(),
    month: 9,
    year: 2020,
    value: 333,
  ),
  TestingLoopingModel(
    id: DateTime.now().toString(),
    month: 11,
    year: 2020,
    value: 2222,
  ),
];

I want Month in my listTest convert to Month Name Something Like January,Feburary,March,April,Mei,etc.... How can i do this ?

I know i can use this Intl Packages like this DateFormat.MMMM().format(???); But format required DateTime and in my list Month is integer.

Thank's

View

SizedBox(
              height: sizes.height(context) / 1.7,
              child: ListView.separated(
                separatorBuilder: (ctx, index) => Divider(),
                itemCount: 12,
                shrinkWrap: true,
                itemBuilder: (BuildContext context, int index) {
                  final indexOrder = index + 1;

                  final result = listTest[index];
                  listTest.sort((a, b) => a.month.compareTo(b.month));
                  return InkWell(
                    onTap: () => showDialog(
                      context: context,
                      builder: (ctxDialog) => FormElectricTenant(),
                    ),
                    child: Padding(
                      padding: const EdgeInsets.symmetric(vertical: 8.0),
                      child: Row(
                        children: <Widget>[
                          titleHeaderRow(title: '$indexOrder' ?? '0'),
                          titleHeaderRow(title: '${result.month}' ?? '0'),
                          titleHeaderRow(title: '${result.value}' ?? '0'),
                        ],
                      ),
                    ),
                  );
                },
              ),
            ),

enter image description here

4 Answers

Is this you want ?

replace month with you month int value

DateFormat('MMMM').format(DateTime(0, month))

Value: January,Feburary ...

DateFormat.MMMM().format(date)

Simple and allows future localization. You can put your locale inside of .MMMM(). This is the best and most scalable solution.

const Map<int,String> monthsInYear = {
 1: "January",
 2: "February",
 3: "March",
 4: “April”,
 5: “May”,
 6: “June”
 …
 12: "December"
}

In Your ListView

ListView.separated(
        separatorBuilder: (ctx, index) => Divider(),
        itemCount: 12,
        shrinkWrap: true,
        itemBuilder: (BuildContext context, int index) {
          final indexOrder = index + 1;

          final result = listTest[index];
          listTest.sort((a, b) => a.month.compareTo(b.month));
          return InkWell(
            onTap: () => showDialog(
              context: context,
              builder: (ctxDialog) => FormElectricTenant(),
            ),
            child: Padding(
              padding: const EdgeInsets.symmetric(vertical: 8.0),
              child: Row(
                children: <Widget>[
                  titleHeaderRow(title: '$indexOrder' ?? '0'),
                  titleHeaderRow(title: '${monthsInYear[result.month]}' ?? '---'),
                  titleHeaderRow(title: '${result.value}' ?? '0'),
                ],
              ),
            ),
          );
        },
      );

Okay I get it, Like Gaurav Mall said, I create a new DateTime object and change it like this

                  final indexOrder = index + 1;
                  final now = DateTime.now();
                  final result = listTest[index];
                  final month = DateTime(now.year, indexOrder, now.day);
                  final convertMonth =         DateFormat.MMMM(appConfig.indonesiaLocale).format(month);
                  print(convertMonth);

View

SizedBox(
              height: sizes.height(context) / 1.7,
              child: ListView.separated(
                separatorBuilder: (ctx, index) => Divider(),
                itemCount: 12,
                shrinkWrap: true,
                itemBuilder: (BuildContext context, int index) {
                  final indexOrder = index + 1;
                  final now = DateTime.now();
                  final result = listTest[index];
                  final month = DateTime(now.year, indexOrder, now.day);
                  final convertMonth =
                      DateFormat.MMMM(appConfig.indonesiaLocale).format(month);
                  print(convertMonth);
                  listTest.sort((a, b) => a.month.compareTo(b.month));
                  return InkWell(
                    onTap: () => showDialog(
                      context: context,
                      builder: (ctxDialog) => FormElectricTenant(),
                    ),
                    child: Padding(
                      padding: const EdgeInsets.symmetric(vertical: 8.0),
                      child: Row(
                        children: <Widget>[
                          titleHeaderRow(title: '$indexOrder' ?? '0'),
                          titleHeaderRow(title: '$convertMonth' ?? '0'),
                          titleHeaderRow(title: '${result.value}' ?? '0'),
                        ],
                      ),
                    ),
                  );
                },
              ),
            ),
Related