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

swiper item can't be hot add? #64

Closed
ytxDaXiong opened this issue Mar 18, 2019 · 16 comments
Closed

swiper item can't be hot add? #64

ytxDaXiong opened this issue Mar 18, 2019 · 16 comments

Comments

@ytxDaXiong
Copy link

I download img from server then add the new item into the swiper items List.
next setState() but it give me an error like this.
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (21956): The following assertion was thrown building NotificationListener:
I/flutter (21956): ScrollController not attached to any scroll views.
I/flutter (21956): 'package:flutter/src/widgets/scroll_controller.dart': Failed assertion: line 110 pos 12:
I/flutter (21956): '_positions.isNotEmpty'
I/flutter (21956):
I/flutter (21956): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter (21956): more information in this error message to help you determine and fix the underlying cause.
I/flutter (21956): In either case, please report this assertion by filing a bug on GitHub:
I/flutter (21956): https://github.com/flutter/flutter/issues/new?template=BUG.md
I/flutter (21956):
I/flutter (21956): When the exception was thrown, this was the stack:
I/flutter (21956): #2 ScrollController.position (package:flutter/src/widgets/scroll_controller.dart:110:12)
I/flutter (21956): #3 PageController.animateToPage (package:flutter/src/widgets/page_view.dart:119:41)
I/flutter (21956): #4 _TransformerPageViewState.didUpdateWidget (package:transformer_page_view/transformer_page_view.dart:511:25)
I/flutter (21956): #5 StatefulElement.update (package:flutter/src/widgets/framework.dart:3879:58)
I/flutter (21956): #6 Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (21956): #7 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (21956): #8 Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (21956): #9 StatelessElement.update (package:flutter/src/widgets/framework.dart:3796:5)
I/flutter (21956): #10 Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (21956): #11 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:4601:32)
I/flutter (21956): #12 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4992:17)
I/flutter (21956): #13 Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (21956): #14 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (21956): #15 Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (21956): #16 StatefulElement.update (package:flutter/src/widgets/framework.dart:3894:5)
I/flutter (21956): #17 Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (21956): #18 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:4601:32)
I/flutter (21956): #19 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4992:17)
I/flutter (21956): #20 Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (21956): #21 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:4883:14)
I/flutter (21956): #22 Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (21956): #23 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (21956): #24 Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (21956): #25 StatelessElement.update (package:flutter/src/widgets/framework.dart:3796:5)
I/flutter (21956): #26 Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (21956): #27 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (21956): #28 Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (21956): #29 ProxyElement.update (package:flutter/src/widgets/framework.dart:4006:5)
I/flutter (21956): #30 Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)
I/flutter (21956): #31 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
I/flutter (21956): #32 Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
I/flutter (21956): #33 ProxyElement.update (package:flutter/src/widgets/framework.dart:4006:5)
I/flutter (21956): #34 Element.updateChild (package:flutter/src/widgets/framework.dart:2748:15)

@ytxDaXiong
Copy link
Author

I found the swiper.js has observer can update item, if the package can do it?

@ytxDaXiong
Copy link
Author

I found that if the new itemCount is the same as the init count. it's will play normal

@zhouxting
Copy link

Before setState,use method stopAutoplay and method move(0) of swiperController

@ytxDaXiong
Copy link
Author

Before setState,use method stopAutoplay and method move(0) of swiperController

I have try, but it doesn't work and return the same error

@zhouxting
Copy link

The following is what I write and it works

Widget build(BuildContext context) {
_controller.stopAutoplay();
_controller.move(0);
var widget= new Container(
width: MediaQuery.of(context).size.width,
height: 200.0,
child:Builder(builder: (context){
return new ScopedModelDescendant(
builder: (context,widget,model){
return model.imageFiles.length>0? new Swiper(
autoplay: true,
itemBuilder: (BuildContext context, int index) {
if(model.imageFiles[index]!=null){
return model.imageFiles[index];
}else{
return new Image.asset("assets/images/loading.jpg");
}
},
itemCount:model.imageFiles.length,
itemHeight: 200,
itemWidth: MediaQuery.of(context).size.width,
pagination: new SwiperPagination(),
controller: _controller,
):new Image.asset("assets/images/loading.jpg");
},
);
})
);
_controller.startAutoplay();
return widget;
}

@ytxDaXiong
Copy link
Author

The following is what I write and it works

Widget build(BuildContext context) {
_controller.stopAutoplay();
_controller.move(0);
var widget= new Container(
width: MediaQuery.of(context).size.width,
height: 200.0,
child:Builder(builder: (context){
return new ScopedModelDescendant(
builder: (context,widget,model){
return model.imageFiles.length>0? new Swiper(
autoplay: true,
itemBuilder: (BuildContext context, int index) {
if(model.imageFiles[index]!=null){
return model.imageFiles[index];
}else{
return new Image.asset("assets/images/loading.jpg");
}
},
itemCount:model.imageFiles.length,
itemHeight: 200,
itemWidth: MediaQuery.of(context).size.width,
pagination: new SwiperPagination(),
controller: _controller,
):new Image.asset("assets/images/loading.jpg");
},
);
})
);
_controller.startAutoplay();
return widget;
}

thanks a lot but it still does't work for me。if the itemCount changed the program will return the same error

@ytxDaXiong
Copy link
Author

@zhouxting your solutions create swiper after list has value. however, i need to create swiper first when img download finished next to setState();

@ytxDaXiong
Copy link
Author

oh! ,i solve the problem by set different key . i will close the issue

@Tokenyet
Copy link

Tokenyet commented May 5, 2019

I suggested this should be added in Readme, It's easy to happen on hot loading and failed randomly.

@JunwuHuang
Copy link

oh! ,i solve the problem by set different key . i will close the issue

which widget i should set key

@451518849
Copy link

我的解决方案是给Swiper设置一个key,当需要更新item的个数时,根据item的不同长度给Swiper使用不同的key就行,我使用的key是: ValueKey(items.length)

@quanzaiyu
Copy link

I had the same problem and I solved it by same way. Thanks a lot !

@lifelikejuly
Copy link

oh! ,i solve the problem by set different key . i will close the issue

Please what's mean about set diff key to solve this problem. I had the same problem

@DennisMuchiri
Copy link

oh! ,i solve the problem by set different key . i will close the issue

please share the code

@waqas-alii
Copy link

My solution is to set a key for Swiper. When the number of items needs to be updated, use different keys for Swiper according to the different lengths of the items. The key I use is: ValueKey (items.length)

@congni congni mentioned this issue Jun 8, 2020
@poonkuzhalik
Copy link

My solution is to set a key for Swiper. When the number of items needs to be updated, use different keys for Swiper according to the different lengths of the items. The key I use is: ValueKey (items.length)

Works!!! Awesome

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

No branches or pull requests

10 participants