Flutter中如何从列表更新Provider变量?

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

flutter lists and provider how to update provider variables from a list?

问题

I have a list in a changenotifier called my list. this list takes a class called listitem which is two variables for this demonstration called variable 1 and variable 2. here is my list and the listitem class.

List<ListItem> MyList = [
ListItem(
      variable1: 2,
      variable2: true),
  ListItem(
      variable1: 5,
      variable2: false),
  ListItem(
      variable1: 1,
      variable2: true),
  ListItem(
      variable1: 7,
      variable2: true),
  ListItem(
      variable1: 2,
      variable2: false),
  ListItem(
      variable1: 3,
      variable2: true),
];

class ListItem {
  int variable1;
  bool variable2;
  

  ListItem({
    required this.variable1,
    required this.variable2,
    
  });
}

In my UI page, i'm creating a list of tiles using ListView.builder and what I want to happen is that when the button is pressed from the listview, I want it to update a set of variables in a change notifier. (this changenotifier is also where the list is housed.

so in my change notifier I have the following:

class MyProvider extends ChangeNotifier {

int _variable1 = 0;
bool _variable2 = false;

int get variable1 => _variable1;
bool get variable2 => _variable2;

void UpdateList(ListItem index){
_variable1 = MyList[index.variable1].variable1;
_variable2 = MyList[index.variable2].variable2;
    notifyListeners();
}

List<ListItem> MyList = [
ListItem(
      variable1: 2,
      variable2: true),
  ListItem(
      variable1: 5,
      variable2: false),
  ListItem(
      variable1: 1,
      variable2: true),
  ListItem(
      variable1: 7,
      variable2: true),
  ListItem(
      variable1: 2,
      variable2: false),
  ListItem(
      variable1: 3,
      variable2: true),
];

}

so when a button is pressed in the UI, it runs the UpdateList function but the bool and int variable in the changenotifier don't update to what is represented in the ListItem of that choice.

thanks so much and any help would be greatly appreciated.

英文:

I have a list in a changenotifier called my list. this list takes a class called listitem which is two variables for this demonstration called variable 1 and variable 2. here is my list and the listitem class.

List<ListItem> MyList = [
ListItem(
      variable1: 2,
      variable2: true),
  ListItem(
      variable1: 5,
      variable2: false),
  ListItem(
      variable1: 1,
      variable2: true),
  ListItem(
      variable1: 7,
      variable2: true),
  ListItem(
      variable1: 2,
      variable2: false),
  ListItem(
      variable1: 3,
      variable2: true),
];

class ListItem {
  int variable1;
  bool variable2;
  

  ListItem({
    required this.variable1,
    required this.variable2,
    
  });
}


In my UI page, i'm creating a list of tiles using ListView.builder and what I want to happen is that when the button is pressed from the listview, I want it to update a set of variables in a change notifier. (this changenotifier is also where the list is housed.

so in my change notifier I have the following:

class MyProvider extends ChangeNotifier {

int _variable1 = 0;
bool _variable2 = false;

int get variable1 => _variable1;
bool get variable2 => _variable2;

void UpdateList(ListItem index){
_variable1 = MyList[index.variable1].variable1;
_variable2 = MyList[index.variable2].variable2;
    notifyListeners();
}

List<ListItem> MyList = [
ListItem(
      variable1: 2,
      variable2: true),
  ListItem(
      variable1: 5,
      variable2: false),
  ListItem(
      variable1: 1,
      variable2: true),
  ListItem(
      variable1: 7,
      variable2: true),
  ListItem(
      variable1: 2,
      variable2: false),
  ListItem(
      variable1: 3,
      variable2: true),
];

}

so when a button is pressed in the UI, it runs the UpdateList function but the bool and int variable in the changenotifier don't update to what is represented in the ListItem of that choice.

thanks so much and any help would be greatly appreciated.

答案1

得分: 1

你应该更改你的方法以更新MyProvider中的变量。
在Dart中有一个提示,使用lowerCamelCase来命名方法和变量:

void updateList(ListItem item) {
  _variable1 = item.variable1;
  _variable2 = item.variable2;
  notifyListeners();
}

在你的页面中,当你想要更新你的列表时,使用你的提供者的updateList方法,并将选定的ListItem作为其参数传递。

如果你有任何问题,请提出。祝编程愉快...

英文:

You should change your method to this to update the variables in MyProvider.
There is a note to notify in Dart to use lowerCamelCase for method and variable names:

void updateList(ListItem item) {
  _variable1 = item.variable1;
  _variable2 = item.variable2;
  notifyListeners();
}

and in your page when you want to update your list use updateList method of your provider and pass the selected ListItem as its arg.

if you had any question please ask.

Happy coding...

huangapple
  • 本文由 发表于 2023年6月27日 20:52:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76565062.html
匿名

发表评论

匿名网友

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

确定