nixrajput / flutter_carousel_widget

A customizable Flutter carousel widget with infinite scrolling, auto-scrolling, pre-built indicators, expandable widgets, auto-sized child support, and enlarged center page.
https://pub.dev/packages/flutter_carousel_widget
MIT License
34 stars 20 forks source link

how remove 'setState' error log? #20

Closed minjinDelable closed 1 year ago

minjinDelable commented 1 year ago

i find error log log here

======== Exception caught by foundation library ====================================================
The following assertion was thrown while dispatching notifications for PageController:
setState() or markNeedsBuild() called during build.

This FlutterCarousel 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: FlutterCarousel
  dependencies: [ScrollConfiguration]
  state: FlutterCarouselState#ffde5
The widget which was currently being built when the offending call was made was: NotificationListener<ScrollNotification>
When the exception was thrown, this was the stack: 
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 266:49      throw_
packages/flutter/src/widgets/framework.dart 4634:9                                <fn>
packages/flutter/src/widgets/framework.dart 4645:14                               markNeedsBuild
packages/flutter/src/widgets/framework.dart 1153:5                                setState
packages/flutter_carousel_widget/src/_flutter_carousel_widget.dart 86:7           <fn>
packages/flutter/src/foundation/change_notifier.dart 381:24                       notifyListeners
packages/flutter/src/foundation/change_notifier.dart 381:24                       notifyListeners
packages/flutter/src/widgets/scroll_position.dart 984:11                          notifyListeners
packages/flutter/src/widgets/scroll_position.dart 384:5                           forcePixels
packages/flutter/src/widgets/page_view.dart 367:7                                 set viewportFraction
packages/flutter/src/widgets/page_view.dart 259:17                                attach
packages/flutter/src/widgets/scrollable.dart 561:34                               didUpdateWidget

i don`t use setState

but this package useful setState

_pageController!.addListener(() {
      setState(() {
        _currentPage = _pageController!.page!.floor();
        _pageDelta = _pageController!.page! - _pageController!.page!.floor();
      });
    });
 _pageController!.addListener(() {
      setState(() {
        _currentPage = _pageController!.page!.floor();
        _pageDelta = _pageController!.page! - _pageController!.page!.floor();
      });
    });
  }

how delete error log ?

I don't want it.

minjinDelable commented 1 year ago

i am flutter web develop i use this version 'flutter_carousel_widget: ^2.0.2' my flutter version 3.7.12

wcfv commented 1 year ago

I'm getting this error too. To reproduce:

set CarouselOptions.viewportFraction to be calculated based on screen width, and resize the screen. You'll get this error.

This FlutterCarousel 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: FlutterCarousel
    dependencies: [ScrollConfiguration]
    state: FlutterCarouselState#5d7bd
The widget which was currently being built when the offending call was made was: NotificationListener<ScrollNotification>
When the exception was thrown, this was the stack
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 288:49      throw_
packages/flutter/src/widgets/framework.dart 4651:9                                <fn>
packages/flutter/src/widgets/framework.dart 4662:14                               markNeedsBuild
packages/flutter/src/widgets/framework.dart 1159:5                                setState
packages/flutter_carousel_widget/src/_flutter_carousel_widget.dart 86:7           <fn>
2
packages/flutter/src/foundation/change_notifier.dart 403:24                       notifyListeners
packages/flutter/src/widgets/scroll_position.dart 965:11                          notifyListeners
packages/flutter/src/widgets/scroll_position.dart 384:5                           forcePixels
packages/flutter/src/widgets/page_view.dart 364:7                                 set viewportFraction```

It probably needs a post-frame callback to fix it?