best-flutter / flutter_swiper

The best swiper for flutter , with multiple layouts, infinite loop. Compatible with Android & iOS.
MIT License
3.51k stars 715 forks source link

Adding and Removing pages in Swiper #20

Open fvisticot opened 6 years ago

fvisticot commented 6 years ago

I need to add / remove page in the Swiper with an Action Button (button on the bottom of the page). -> Action add: Add a page at the end of the Swiper and move the Swiper to the index of the new created page => seems OK -> Action remove: Remove current page and display Swiper without the deleted page => OK but NOT when the last page of the Swiper is deleted (see exception in the traces)

Implementation used:

Traces when last page is removed:

flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ flutter: The following assertion was thrown building Swiper(state: _SwiperState#25085): flutter: ScrollController not attached to any scroll views. flutter: 'package:flutter/src/widgets/scroll_controller.dart': Failed assertion: line 110 pos 12: flutter: '_positions.isNotEmpty' flutter: flutter: Either the assertion indicates an error in the framework itself, or we should provide substantially flutter: more information in this error message to help you determine and fix the underlying cause. flutter: In either case, please report this assertion by filing a bug on GitHub: flutter: https://github.com/flutter/flutter/issues/new flutter: flutter: When the exception was thrown, this was the stack: flutter: #2 ScrollController.position (package:flutter/src/widgets/scroll_controller.dart:110:12) flutter: #3 PageController.animateToPage (package:flutter/src/widgets/page_view.dart:118:41) flutter: #4 _TransformerPageViewState.didUpdateWidget (package:transformer_page_view/transformer_page_view.dart:508:25) flutter: #5 StatefulElement.update (package:flutter/src/widgets/framework.dart:3820:58) flutter: #6 Element.updateChild (package:flutter/src/widgets/framework.dart:2729:15) flutter: #7 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:4521:32) flutter: #8 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4911:17) flutter: #9 Element.updateChild (package:flutter/src/widgets/framework.dart:2729:15) flutter: #10 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3689:16) flutter: #11 Element.rebuild (package:flutter/src/widgets/framework.dart:3531:5) flutter: #12 StatefulElement.update (package:flutter/src/widgets/framework.dart:3835:5) flutter: #13 Element.updateChild (package:flutter/src/widgets/framework.dart:2729:15) flutter: #14 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3689:16) flutter: #15 Element.rebuild (package:flutter/src/widgets/framework.dart:3531:5) flutter: #16 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2273:33) flutter: #17 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:673:20) flutter: #18 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:208:5) flutter: #19 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:990:15) flutter: #20 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:930:9) flutter: #21 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:842:5) flutter: #22 _invoke (dart:ui/hooks.dart:139:13) flutter: #23 _drawFrame (dart:ui/hooks.dart:128:3)

sbesnard commented 5 years ago

Same problem here ! You can avoid it by saving a reference to the SwiperController and call its previous method just before deleting the widget (Future.delayed() may come handy...)

sbesnard commented 5 years ago

Actually, the problem occurs when deleting the first element as well, and I have no solution for that yet.

Xingefb commented 5 years ago

嵌套在CustomScrollView使用抛出异常,进行swiper count 判断,如果数量为空则不显示,数量不为空在显示 if (null == _swipers || _swipers.isEmpty) ? SizedBox() : _swiper() ;

deimantasa commented 5 years ago

Hey, was this issue fixed? As I face exactly same thing. I receive an error ScrollController not attached to any scroll views. 'package:flutter/src/widgets/scroll_controller.dart': Failed assertion: line 110 pos 12: '_positions.isNotEmpty' After I remove an item from Swiper.

Cheers

yousifAlneamy commented 5 years ago

Hey, I was able to solve this by dispose/recreate the wrapper widget of the swiper() by giving a UniqueKey each time I was going to pass data to the swiper.

the state is maintained in the parent widget, sample code:

MySwiperWrapper(key: UniqueKey(), images: images, ......)

class MySwiperWrapper extends StatefulWidget { ....... MySwiperWrapper ({ @required Key key, @required this.images, }) : super(key: key); ..... }

NOTE: it's not an efficient solution, but it works

@zeromaro, hope this helps you

ZeroMoment commented 4 years ago

Hey, I was able to solve this by dispose/recreate the wrapper widget of the swiper() by giving a UniqueKey each time I was going to pass data to the swiper.

the state is maintained in the parent widget, sample code:

MySwiperWrapper(key: UniqueKey(), images: images, ......)

class MySwiperWrapper extends StatefulWidget { ....... MySwiperWrapper ({ @required Key key, @required this.images, }) : super(key: key); ..... }

NOTE: it's not an efficient solution, but it works

@zeromaro, hope this helps you

it's worked,but i don't know why

him123 commented 4 years ago

Hey, I was able to solve this by dispose/recreate the wrapper widget of the swiper() by giving a UniqueKey each time I was going to pass data to the swiper.

the state is maintained in the parent widget, sample code:

MySwiperWrapper(key: UniqueKey(), images: images, ......)

class MySwiperWrapper extends StatefulWidget { ....... MySwiperWrapper ({ @required Key key, @required this.images, }) : super(key: key); ..... }

NOTE: it's not an efficient solution, but it works

It works....thank you so much

holtalanm commented 3 years ago

Hey, I was able to solve this by dispose/recreate the wrapper widget of the swiper() by giving a UniqueKey each time I was going to pass data to the swiper.

the state is maintained in the parent widget, sample code:

MySwiperWrapper(key: UniqueKey(), images: images, ......)

class MySwiperWrapper extends StatefulWidget { ....... MySwiperWrapper ({ @required Key key, @required this.images, }) : super(key: key); ..... }

NOTE: it's not an efficient solution, but it works

@zeromaro, hope this helps you

this fixed my issue, as well. thanks!

tamnhoskins commented 2 years ago

I am using the SwipperController with the Swiper object and calling the move function, which was causing the "ScrollController not attached to any scroll views" exception. Adding a unique key to my Swiper object did not resolve the exception. What finally fixed the problem for me was setting the animation field to false when move is called: swiperController.move(provider.getCurrentActiveIndex(), animation: false);