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

NoSuchMethodError: Class 'DateTime' has no instance getter 'isNotEmpty'. #142

Closed
awhitford opened this issue Sep 23, 2019 · 5 comments
Closed

Comments

@awhitford
Copy link
Collaborator

Running into a strange problem... Have a field like:

                    FormBuilderDateTimePicker(
                      attribute: "birthday",
                      inputType: InputType.date,
                      initialDatePickerMode: DatePickerMode.year,
                      initialDate: DateTime.now().subtract(Duration(days:18*365)),
                      lastDate: DateTime.now().subtract(Duration(days:13*365)),
                      decoration: InputDecoration(
                          icon: Icon(Icons.cake),
                          labelText: "Birthday (optional)"),
                      format: DateFormat("MMM dd, yyyy"),
                      validators: [FormBuilderValidators.date()],
                    ),

When this field is empty, there is no form submission problem. However, when a date is specified, then I get an exception:

[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: Class 'DateTime' has no instance getter 'isNotEmpty'.
Receiver: Instance of 'DateTime'
Tried calling: isNotEmpty
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1      FormBuilderValidators.date.<anonymous closure> (package:flutter_form_builder/src/form_builder_validators.dart:192:52)
#2      _FormBuilderDateTimePickerState.build.<anonymous closure> (package:flutter_form_builder/src/fields/form_builder_date_time_picker.dart:274:35)
#3      FormFieldState._validate (package:flutter/src/widgets/form.dart)
#4      FormFieldState.validate.<anonymous closure> (package:flutter/src/widgets/form.dart:364:7)
#5      State.setState (package:flutter/src/widgets/framework.dart:1141:30)
#6      FormFieldState.validate (package:flutter/src/widgets/form.dart:363:5)
#7      FormState._validate (package:flutter/src/widgets/form.dart:203:25)
#8      FormState.validate (package:flutter/src/widgets/form.dart:197:12)
#9      F<…>

Please note that I am using version 3.5.4, Flutter 1.9.1+hotfix.2, and the iOS Simulator (Version 11.0).

@awhitford
Copy link
Collaborator Author

It appears to be related to this code:

  /// [FormFieldValidator] that requires the field's value to be a valid date string.
  static FormFieldValidator date({
    String errorText = "This field requires a valid date string.",
  }) {
    return (valueCandidate) {
      if (valueCandidate != null && valueCandidate.isNotEmpty) {
        if (!isDate(valueCandidate)) return errorText;
      }
      return null;
    };
  }

valueCandidate must be a DateTime rather than a String, so the isNotEmpty does not exist.

💡 Note that this may be related to the fact that I am using initialDate and/or lastDate because I do not recall running into this before and the validation code has not changed.

@awhitford
Copy link
Collaborator Author

I confirmed that the issue has nothing to do with using the initialDate or lastDate because if I comment that out, it still trips on this exception. When a date is selected, the value is a DateTime (not a String), so the isNotEmpty method does not exist.

Am I doing something wrong? Or does the date validator logic need to be tweaked?

@awhitford
Copy link
Collaborator Author

🤔 Is the validator completely redundant in this case? Can I trust that the Date Picker will ALWAYS yield a valid Date (or null/blank)? (If so, then I should simply remove the validator.)

@danvick
Copy link
Collaborator

danvick commented Sep 23, 2019

Hi @awhitford,
In this case the validator is irrelevant since FormBuilderDateTimePicker always gives a DateTime value.

The validator is supposed to be used to validate that a String conforms to a valid date string.

Maybe we should rename the validator from FormBuilderValidators.date() to FormBuilderValidators.dateString()

@awhitford
Copy link
Collaborator Author

Thank you for confirming.

A rename isn't necessary -- just a comment for the developer is likely enough.

@danvick danvick closed this as completed Oct 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants