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

Bug Fix: Bug when setting initialDate to December in ScrollWheelDatePicker #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

vietdq2701
Copy link

@vietdq2701 vietdq2701 commented Dec 25, 2024

Fixes #9

Problem

When setting initialDate to a date in December, the ScrollWheelDatePicker throws an error due to state changes being triggered during the widget's build phase. This issue occurs because the DateController calls notifyListeners directly during initialization.

Solution

  • Added a WidgetsBinding.instance.addPostFrameCallback to delay notifyListeners until after the current frame is built. This prevents the error caused by state changes during the build phase.
  • Updated _dayController initialization to properly handle the month value, ensuring December (month 12) is processed correctly.

Impact

  • Fixes the crash when initialDate is set to December.
  • Improves stability by ensuring notifyListeners is only called after the build phase is complete.
  • No breaking changes for existing implementations.

Code Changes

Before (causing the error):

_dayController = _DayController(
  selectedIndex: initialDate != null ? initialDate.day - 1 : null,
  numberOfDays: _getNumberOfDays(
    year: initialDate?.year ?? DateTime.now().year,
    month: initialDate?.month ?? DateTime.now().month,
  ),
);

  void _updateNumberOfDays() {
    final int numberOfDays = _getNumberOfDays(year: _yearController.selectedIndex, month: _monthController.selectedIndex);

    final int selectedIndex = _dayController.selectedIndex >= numberOfDays ? numberOfDays - 1 : _dayController.selectedIndex;

    _dayController = _dayController.copyWith(selectedIndex: selectedIndex, numberOfDays: numberOfDays);

    notifyListeners();
  }

After fix

_dayController = _DayController(
  selectedIndex: initialDate != null ? initialDate.day - 1 : null,
  numberOfDays: _getNumberOfDays(
    year: initialDate?.year ?? DateTime.now().year,
    month: (initialDate != null) ? initialDate.month - 1 : DateTime.now().month - 1,
  ),
);


void _updateNumberOfDays() {
    final int numberOfDays = _getNumberOfDays(year: _yearController.selectedIndex, month: _monthController.selectedIndex);

    final int selectedIndex = _dayController.selectedIndex >= numberOfDays ? numberOfDays - 1 : _dayController.selectedIndex;

    _dayController = _dayController.copyWith(selectedIndex: selectedIndex, numberOfDays: numberOfDays);

    WidgetsBinding.instance.addPostFrameCallback((_) {
      notifyListeners();
    });
  }
 

@vietdq2701 vietdq2701 changed the title Fix: Bug when setting initialDate to December in ScrollWheelDatePicker Fixes #9 Dec 25, 2024
@vietdq2701 vietdq2701 changed the title Fixes #9 Fix: Bug when setting initialDate to December in ScrollWheelDatePicker Dec 25, 2024
@vietdq2701 vietdq2701 changed the title Fix: Bug when setting initialDate to December in ScrollWheelDatePicker Bug Fix: Bug when setting initialDate to December in ScrollWheelDatePicker Dec 25, 2024
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

Successfully merging this pull request may close these issues.

Error when setting initial date to December
1 participant