英文:
flutter lists and provider how to increase an int value in the list?
问题
我正在使用Listview.builder来基于存储在更改通知器中的列表中的信息构建按钮列表。
以下是列表中包含的项目示例:
ListItem(cost1: 2, cost2: 0, canSelect: 1, timesChosen: 0, name: 'Item 1'),
以下是在listview.builder中构建的对象(按钮)示例:
ListView.builder(
    itemBuilder: (context, index) {
        return InkWell(
            onTap: () {
                context.read<MyProvider>().MyList[index].timesChosen++;
            },
            child: EquipmentButton(
                text: context.read<MyProvider>().MyList[index].name,
                Cost1: '${context.read<MyProvider>().MyList[index].cost1}',
                timesChosen: '${context.read<MyProvider>().MyList[index].timesChosen}',
            ),
        );
    },
    itemCount: context.read<MyProvider>().MyList.length),
当我运行此代码时,它正确地构建了按钮。但是当按下按钮时,我收到以下错误:
类'ListItem'没有实例setter'timesChosen='。
接收器:'ListItem'的实例
尝试调用:timesChosen=1
如何使timesChosen的值增加到列表中?
非常感谢,
英文:
I'm using a Listview.builder to build a list of buttons based on information from a list which is housed in a change notifier.
here is an example of the items contained in the list:
ListItem(cost1: 2, cost2: 0, canSelect: 1, timesChosen: 0, name: 'Item 1'),
and here is an example of the object (button) that is being built in the listview.builder:
ListView.builder(
                  itemBuilder: (context, index) {
                    return InkWell(
                      onTap: () {
                        context.read<MyProvider>().MyList[index].timesChosen++;
                      },
                      child: EquipmentButton(
                          text: context.read<MyProvider>().MyList[index].name,
                          Cost1: '${context.read<MyProvider>().MyList[index].cost1}',
                          timesChosen: '${context.read<MyProvider>().MyList[index].timesChosen}',
                     ),
                    );
                  },
                  itemCount: context.read<MyProvider>().MyList.length),
when I run this code, It builds the buttons correctly. but when a button is pressed, I get the below error:
Class 'ListItem' has no instance setter 'timesChosen='.
Receiver: Instance of 'ListItem'
Tried calling: timesChosen=1
How do i get it to increase the value of timesChosen in the list?
thanks so much,
答案1
得分: 0
这里的问题是您尝试使用timesChosen++表达式来更新ListItem实例的timesChosen属性,这假定该属性具有setter方法。为使您的代码工作,您应确保ListItem类具有timesChosen属性的setter方法。以下是一个带有setter的示例:
class ListItem {
  int cost1;
  int cost2;
  int canSelect;
  int _timesChosen; // 保存timesChosen的值
  String name;
  // timesChosen的Getter方法
  int get timesChosen => _timesChosen;
  // timesChosen的Setter方法
  set timesChosen(int value) {
    _timesChosen = value;
  }
  ListItem(this._timesChosen, {required this.cost1, required this.cost2, required this.canSelect, required this.name});
}
上面的代码片段应该与您的listview中的代码一起工作。
英文:
The problem here is that you are trying to update the timesChosen property of an instance of ListItem using the timesChosen++ expression, which assumes the presence of a setter for that property. For you code to work, you should ensure that the ListItem class has a setter method for the timesChosen property. Below is an example with the setter:
class ListItem {
  int cost1;
  int cost2;
  int canSelect;
  int _timesChosen; // hold the value of timesChosen
  String name;
  // Getter for timesChosen
  int get timesChosen => _timesChosen;
  // Setter for timesChosen
  set timesChosen(int value) {
    _timesChosen = value;
  }
  ListItem(this._timesChosen, {required this.cost1, required this.cost2, required this.canSelect, required this.name});
}
The above code snippet should work with the code in your listview.
答案2
得分: 0
我已经弄清楚了。只需在Listitem类中添加一个void函数。
class EquipmentAndCosts {
  int cost1;
  int cost2;
  int canSelect;
  int timesChosen;
  String name;
  void timesChosenIncrease() {
    timesChosen++;
  }
  EquipmentAndCosts(
      {required this.name,
      required this.cost1,
      required this.cost2,
      required this.canSelect,
      required this.timesChosen});
}
英文:
figured it out. just had to add a void function in the Listitem class.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
class EquipmentAndCosts {
  int cost1;
  int cost2;
  int canSelect;
  int timesChosen;
  String name;
  void timesChosenIncrease() {
    timesChosen++;
  }
  EquipmentAndCosts(
      {required this.name,
      required this.cost1,
      required this.cost2,
      required this.canSelect,
      required this.timesChosen});
}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论