flutter-form-builder-ecosystem / flutter_form_builder

Simple form maker for Flutter Framework
https://pub.dev/packages/flutter_form_builder
MIT License
1.45k stars 526 forks source link

[FormBuilderDateTimePicker]: Got problem if initialDate is null and lastDate is years ago. #1368

Open devon opened 4 months ago

devon commented 4 months ago

Is there an existing issue for this?

Package/Plugin version

9.2.1

Platforms

Flutter doctor

Flutter doctor ```bash Flutter (Channel stable, 3.16.9, on macOS 14.2.1 23C71 darwin-arm64, locale en-CA) [✓] Xcode - develop for iOS and macOS (Xcode 15.2) [✓] Chrome - develop for the web [✓] Android Studio (version 2023.1) ```

Minimal code example

Code sample ```dart FormBuilderDateTimePicker( name: 'birthday', initialValue: null, firstDate: DateTime(1923, 1, 1), lastDate: DateTime(2006, 12, 31), ), ```

Current Behavior

Got error:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: 'package:flutter/src/material/date_picker.dart': Failed assertion: line 201 pos 5: 'initialDate == null || !initialDate.isAfter(lastDate)': initialDate 2024-02-13 00:00:00.000 must be on or before lastDate 2006-12-31 00:00:00.000.

Expected Behavior

The code should work for a null initialDate.

Steps To Reproduce

Add the form:

FormBuilderDateTimePicker(
  name: 'birthday',
  initialValue: null,
  firstDate: DateTime(1923, 1, 1),
  lastDate: DateTime(2006, 12, 31),
),

And click the field to select a date.

Aditional information

In _showDatePicker, if there is no initialDate, it will set to DateTime.now():

  Future<DateTime?> _showDatePicker(
      BuildContext context, DateTime? currentValue) {
    return showDatePicker(
      context: context,
      selectableDayPredicate: widget.selectableDayPredicate,
      initialDatePickerMode: widget.initialDatePickerMode,
      initialDate: currentValue ?? widget.initialDate ?? DateTime.now(),
     ....

So, it can't pass the assert:

assert(
    initialDate == null || !initialDate.isAfter(lastDate),
    'initialDate $initialDate must be on or before lastDate $lastDate.',
  );