如何在ListView.builder中从API加载数据

huangapple go评论59阅读模式
英文:

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&lt;NewsPage&gt;
    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

以下是根据我理解的问题提供的可能答案(数据加载不够快):

  1. 在一个 List<Widget> 中本地存储数据,然后将其传递给 ListView.builder

  2. 如果希望数据在滚动时保持不变,可以将 ListView.builder 中的 shrinkWrap 设置为 false。

  3. 如果希望列表数据在滚动时从互联网加载,可以尝试使用 ScrollController。有关更多信息,请参考此答案

英文:

Here are some possible answers from my understanding of the question (data not loading fast enough)

  1. Store data locally in a List&lt;Widget&gt; then pass it to the ListView.builder.

  2. Set shrinkWrap in the ListView.builder to false if you want the data to persist for scrolling

  3. 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.

huangapple
  • 本文由 发表于 2023年5月14日 01:37:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定