sobimor / flutter_date_picker_timeline

Gregorian and Jalali customizable date picker as a horizontal timeline
https://pub.dev/packages/flutter_date_picker_timeline
MIT License
34 stars 9 forks source link

Error on setState() #7

Open Alijaaan opened 3 years ago

Alijaaan commented 3 years ago

Hi friend, Thanks for your nice package, but I have a problem, When I'm doing setState in onSelectedDateChange I have the error :

The following assertion was thrown building Container(constraints: BoxConstraints(0.0<=w<=Infinity, h=35.0)): setState() or markNeedsBuild() called during build. This AddNewEventPage widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.

sobimor commented 3 years ago

Hi Ali. Thank you for creating issue.

Could you share the onSelectedDateChange part of your code?

Alijaaan commented 3 years ago

Hi Ali. Thank you for creating issue.

Could you share the onSelectedDateChange part of your code?

Hi dear Moradi, many thanks for your reply,

this is how I used date picker :

FlutterDatePickerTimeline( calendarMode: CalendarMode.jalali, startDate: DateTime.now(), endDate: DateTime.now().add(Duration(days: 14)), initialSelectedDate: DateTime.now(), selectedItemBackgroundColor: Colors.purple, itemRadius: 5, onSelectedDateChange: (DateTime dateTime) { setState(() { eventDate = dateTime; }); }, ),

sobimor commented 3 years ago

I tested your code that you commented and see no issue and bug. (Also you could test the setState in FlutterDatePickerTimeline example project).

My guess is that the reason for this problem is the parent of FlutterDatePickerTimeline in your code. According to the error message which you commented in issue, I think the parent of FlutterDatePickerTimeline in your code height is not enough for render or re-render FlutterDatePickerTimeline. If you need smaller FlutterDatePickerTimeline item, instead of using Container with smaller height or width please use below parameters: selectedItemWidth unselectedItemWidth itemHeight

If you can please share more code from your source code to find out the problem.

Alijaaan commented 3 years ago

Dear Moradi, My problem is when I'm doing setState ,it tries to add another list everytime, Even when I removed everything in my page and I used only sample code.

`

This AddNewEventPage widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase. The widget on which setState() or markNeedsBuild() was called was: AddNewEventPage state: _AddNewEventPageState#e3cba The widget which was currently being built when the offending call was made was: Container constraints: BoxConstraints(0.0<=w<=Infinity, h=35.0) The relevant error-causing widget was: Container file:///D:/Alijaan/flutter/event/lib/pages/home/addNewEvent.dart:1178:20 When the exception was thrown, this was the stack:

0 Element.markNeedsBuild. (package:flutter/src/widgets/framework.dart:4217:11)

1 Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:4232:6)

2 State.setState (package:flutter/src/widgets/framework.dart:1108:15)

3 _AddNewEventPageState.dateBox. (package:event/pages/home/addNewEvent.dart:1188:19)

4 _FlutterDatePickerTimelineState._setSelectedDate (package:flutter_date_picker_timeline/src/flutter_date_picker_timeline_widget.dart:254:32)

... `

Maybe it related to my flutter version : Flutter 2.2.1 • channel stable • https://github.com/flutter/flutter.git Framework • revision 02c026b03c (13 days ago) • 2021-05-27 12:24:44 -0700 Engine • revision 0fdb562ac8 Tools • Dart 2.13.1

Alijaaan commented 3 years ago

I found when I'm setting initialSelectedDate I receive this error, but when I'm not setting anything in initialSelectedDate the error will disappear.

Alijaaan commented 3 years ago

Is there any way to solve this problem, or maybe I'm doing something wrong?

sobimor commented 3 years ago

I tested both case and see no error and issue.

If you can please share more code from your source code here or send me in private via email to find out the problem.

crushman1 commented 3 years ago

im having the same issue, i cant setState in onSelectedDateChange the error says setState() or markNeedsBuild() called during build.

crushman1 commented 3 years ago

To get around the issue i used a Timer in the onSelectedDateChange like this :

Timer(Duration(milliseconds: 500), (){ setState(() { });});

iMajeed16 commented 2 years ago

You could use ValueNotifier as the following example, it works with me. https://medium.com/swlh/use-valuenotifier-like-pro-6441d9ddad05

late ValueNotifier<DateTime> timelineDate;

@override
  void initState() {
    super.initState();
    timelineDate = ValueNotifier<DateTime>(DateTime.now());
  }

@override
  void dispose() {
    timelineDate.dispose();
    super.dispose();
  }

FlutterDatePickerTimeline(
...,
...,
 initialSelectedDate: DateTime.now(),
 onSelectedDateChange: (date) {
                  timelineDate.value = date!;
                },
),

// and wrap the widget with the 
 ValueListenableBuilder(
           valueListenable: timelineDate,
           builder: (context, value, child) {
             return Text(timelineDate.value.toString());
           }
 );

hope this would work.

aliazizi77 commented 2 years ago

I had the same problem. You will get this error if you type initialSelectedDate as follows

initialFocusedDate: DateTime.now(),

To avoid this error, set the initialSelectedDate as follows

initialSelectedDate: DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day),