英文:
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()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论