Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ? conditional in expressions #61

Closed
matthewtsmith opened this issue Nov 14, 2023 · 1 comment
Closed

Support ? conditional in expressions #61

matthewtsmith opened this issue Nov 14, 2023 · 1 comment

Comments

@matthewtsmith
Copy link

matthewtsmith commented Nov 14, 2023

I have added some Date operators as MemberAccessors that are the following:

var dateStringFunctions = MemberAccessor<String>({
  'toDate': (i) => () {
        return parseDate(i);
      },
});

var dateNumFunctions = MemberAccessor<num>({
  'toDate': (i) => () {
        return DateTime.fromMillisecondsSinceEpoch(i.toInt());
      },
});

var dateFunctions = MemberAccessor<DateTime>({
  'format': (date) => (String format) {
        if (date == invalidDate) {
          return '';
        }
        if (format.isEmpty) {
          format = 'date_long';
        }
        var locale = PlatformDispatcher.instance.locale.languageCode;
        var dateFormat24 = PlatformDispatcher.instance.alwaysUse24HourFormat;
        DateFormat formatter;
        try {
          switch (format) {
            case 'date_short':
              formatter = DateFormat.yMd(locale);
              break;
            case 'date_long':
              formatter = DateFormat.yMMMd(locale);
              break;
            case 'date_time_short':
              formatter = DateFormat.yMd(locale);
              if (dateFormat24) {
                formatter = formatter.add_Hm();
              } else {
                formatter = formatter.addPattern("h:mm a");
              }
              break;
            case 'time':
              formatter = DateFormat.Hms(locale);
              break;
            default:
              formatter = DateFormat(format);
          }

          var str = formatter.format(date.toLocal());
          return str;
        } catch (e, s) {
          logError('Invalid date format', e, s);
          return "date format invalid";
        }
      }
});

I can then use an expression like the following:

record.myStringifiedDate.toDate().format('date_short')

However, when the myStringifiedDate field is null it throws an exception. I've attempted to use an expression like

record.myStringifiedDate?.toDate().format('date_short') ?? ''

When I try that I receive the error type 'Null' is not a subtype of type 'Function'

I'm wondering if this is possible and I'm just writing it incorrectly or if support for ? can be added. If you see a way for it to be added I'm happy to contribute if you can provide me some guidance on how to implement this feature.

I see that this can be worked around by using ${record.date != null ? record.date.toDate().format('date_short') : ''} but I'm looking for a more succinct way to write this since null values are common in our system. I can also envision having member functions that could return null such as toDate returning null because the date does not match a supported format.

Copy link

stale bot commented May 12, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot closed this as completed Jun 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant