Skip to content

BUG: TableCalendar Widget Crashes When focusedDay is Out of Range #132

@DeveloperAmrit

Description

@DeveloperAmrit

Is there an existing issue for this?

  • I have searched the existing issues

What happened?

📌 Issue Overview

The TableCalendar widget crashes with a failed assertion error when the focusedDay is outside the range defined by firstDay and lastDay. This issue occurs because the focusedDay is initialized to DateTime.now(), which may not always fall within the hardcoded date range.

🔍 Steps to Reproduce

  1. Set up a TableCalendar widget with a hardcoded date range, e.g.:
TableCalendar(
  firstDay: DateTime.utc(2024, 1, 1),
  lastDay: DateTime.utc(2025, 12, 31),
  focusedDay: DateTime.now(),
  ...
)
  1. Run the app on a date outside the specified range (e.g., DateTime.now() is 2026-01-17).
  2. Observe the app crash with the following error:
'package:table_calendar/src/table_calendar_base.dart': 
Failed assertion: line 78 pos 16: 
'isSameDay(focusedDay, lastDay) || focusedDay.isBefore(lastDay)': is not true.

🎯 Expected Behavior

The TableCalendar widget should handle dynamic date ranges and not crash when the focusedDay is outside the range. The widget should either:

  • Automatically adjust the focusedDay to the nearest valid date within the range.
  • Allow dynamic date ranges to prevent the need for hardcoding.

🚨 Actual Behavior

The app crashes with a failed assertion error when the focusedDay is outside the firstDay and lastDay range.

📷 Screenshot

Image

💡 Suggested Improvements

  • Use a dynamic date range for firstDay and lastDay to ensure the calendar always includes the current date
  • Add an onPageChanged callback to update the focusedDay dynamically as the user navigates through the calendar.

Record

  • I agree to follow this project's Code of Conduct
  • I want to work on this issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions