Flutter, 在列表视图中重新加载一个项目

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

Flutter, reload an item in List View

问题

这应该很普通,但我找不到太多关于这个的信息。我有一个使用卡片的惰性加载列表,用于显示从http请求中获取的每个记录的数据。当点击卡片中的按钮或执行操作时,如何刷新单个记录的数据?

结构类似于:

Stateful Widget
    loadData()  // 通过页面加载的http请求

    Widget build
        TextButton onPressed(){loadData} 
        ListView.builder(
            itemCount: fullList.length,
            itemBuilder: (BuildContext context, int index) {
            return dataCard(context, fullList, index);
        }),
dataCard(context, dl, i)
    Text(dl[i].Name)
    Text(dl[i].Price) 
    setIcon(dl[i].type)
    ...
setIcon(type)
    switch type
    case 1
        return Icon A
    case 2
        return Icon B
    ...

我如何检测dataCard或setIcon中的操作并重新加载列表中相应的项目?

英文:

This should be common but I can't find much information about this. I have a lazy loading list with Cards to show data for each record fetched from http request. How do I refresh the data of a single record when a button or action in a Card is being clicked or done?

Structure is something like:

Stateful Widget
    loadData()  //http request by page loading

    Widget build
        TextButton onPressed(){loadData} 
        ListView.builder(
            itemCount: fullList.length,
            itemBuilder: (BuildContext context, int index) {
            return dataCard(context, fullList, index);
        }),
dataCard(context, dl, i)
    Text(dl[i].Name)
    Text(dl[i].Price) 
    setIcon(dl[i].type)
    ...
setIcon(type)
    switch type
    case 1
        return Icon A
    case 2
        return Icon B
    ...

How can I detect action in dataCard or setIcon and reload the corresponding item in the list?

答案1

得分: 0

void refreshRecord(index) async{
dataList[index].name = 新数值
setState()
}

英文:

By adding a function in stateful class, update the data list by index, then setState().

void refreshRecord(index) async{
   dataList[index].name = new value  
   setState()
}

huangapple
  • 本文由 发表于 2023年2月6日 18:02:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359829.html
匿名

发表评论

匿名网友

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

确定