英文:
flutter provider ChangeNotifier class with a call-back function not working
问题
I'm using provider and my code looks like this.
class AbcClass with ChangeNotifier {
String abc1 = '111';
String abc2 = '222';
List<void Function(String)> get abcInputs {
return [
(newValue) {
abc1 = newValue;
notifyListeners();
},
(newValue) {
abc2 = newValue;
notifyListeners();
}
];
}
}
and I'm setting abcInputs (callback function) to UI textfield.
Widget build(BuildContext context) {
context.watch<AbcClass>();
AbcClass abcClass = context.read<AbcClass>();
return TextFormField(
onChanged: abcClass.abcInputs[0],
initialValue: abcClass.abc1.toString(),
);
}
My problem is when I change the input value from UI, abcClass, abc1 value does not change. Can anyone help with that? Appreciate your help.
英文:
I'm using provider and my code looks like this.
class AbcClass with ChangeNotifier {
String abc1='111';
String abc2='222';
List<void Function(String)> get abcInputs {
return [
(newValue) => () {
abc1 = newValue;
notifyListeners();
},
(newValue) => () {
abc2 = newValue;
notifyListeners();
}
];
}
}
and i'm setting abcInputs(callback function) to UI textfield.
Widget build(BuildContext context) {
context.watch<AbcClass>();
AbcClass abcClass= context.read<AbcClass>();
return TextFormField(
onChanged: abcClass.abcInputs[0],
initialValue:abcClass.abc1.toString(),
}
My problem is when i changed the input value from UI,
abcClass , abc1 value not change.
can anyone help to do that.
appreciate your help.
答案1
得分: 0
问题是 ' => ' ,当我移除它时,它能工作。
List<void Function(String)> get abcInputs {
return [
(newValue) () {
abc1 = newValue;
notifyListeners();
},
(newValue) () {
abc2 = newValue;
notifyListeners();
}
];
}
英文:
Problem was ' => ' , when I removed it. it works
List<void Function(String)> get abcInputs {
return [
(newValue) => () {
abc1 = newValue;
notifyListeners();
},
(newValue) => () {
abc2 = newValue;
notifyListeners();
}
];
}
to this
List<void Function(String)> get abcInputs {
return [
(newValue) () {
abc1 = newValue;
notifyListeners();
},
(newValue) () {
abc2 = newValue;
notifyListeners();
}
];
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论