英文:
how to load data from an API inside a ListView.builder
问题
我从API获取新闻列表,使用ListView.builder显示数据...
问题出现在我尝试在列表中向上或向下滚动时,因为先前加载的数据未被保留,如果我滚动得太快,就会发生相同的错误...
我尝试在我的StatefulWidget中使用一个mixin "AutomaticKeepAliveClientMixin",但没有起作用。
class _NewsPageState extends State<NewsPage> with AutomaticKeepAliveClientMixin {
我尝试根据这里提出的另一种解决方案更改build方法
@override
Widget build(BuildContext context) {
super.build(context);
有解决方法吗?
英文:
im obtaining a list o news from an API,im using a ListView.builder to show the data...
the problem comes when i tried to scroll down or up in the list, because previously loaded data is not kept and if i scroll-down to fast the same error happends...
i tried to use in my statefull widget a mixin "AutomaticKeepAliveClientMixin" but does not works.
class _NewsPageState extends State<NewsPage>
with AutomaticKeepAliveClientMixin {
and i tried to change the build method according to another solution proposed here
@override
Widget build(BuildContext context) {
super.build(context);
any solution for this?
答案1
得分: 0
以下是根据我理解的问题提供的可能答案(数据加载不够快):
-
在一个
List<Widget>
中本地存储数据,然后将其传递给ListView.builder
。 -
如果希望数据在滚动时保持不变,可以将
ListView.builder
中的shrinkWrap
设置为 false。 -
如果希望列表数据在滚动时从互联网加载,可以尝试使用
ScrollController
。有关更多信息,请参考此答案。
英文:
Here are some possible answers from my understanding of the question (data not loading fast enough)
-
Store data locally in a
List<Widget>
then pass it to theListView.builder.
-
Set
shrinkWrap
in theListView.builder
to false if you want the data to persist for scrolling -
If you want the list data to load from the internet while scrolling, you can try
ScrollController
Refer to this answer for more on that regard.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论